-
Notifications
You must be signed in to change notification settings - Fork 0
/
sales_window.py
80 lines (66 loc) · 3.54 KB
/
sales_window.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'sales_window.ui'
from PyQt5 import QtCore, QtGui, QtWidgets
from mysql import MySQL # 导入数据库操作文件
from PyQt5.QtGui import QPalette, QPixmap, QColor # 导入调色板、位图、颜色
# 创建自定义数据库对象
mysql = MySQL()
# 连接数据库
sql = mysql.connection_sql()
# 创建游标
cur = sql.cursor()
class Sales_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
MainWindow.setMinimumSize(QtCore.QSize(800, 600))
MainWindow.setMaximumSize(QtCore.QSize(800, 600))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# 开启自动填充背景
self.centralwidget.setAutoFillBackground(True)
palette = QPalette() # 调色板类
palette.setBrush(QPalette.Background,
QtGui.QBrush(QtGui.QPixmap('img/rankings_bg.png'))) # 设置背景图片
self.centralwidget.setPalette(palette) # 为控件设置对应的调色板即可
# 获取销量排行前100名数据信息
row, column, results = mysql.query_top100_rankings(cur, 'sales_volume_rankings')
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(0, 69, 800, 530))
# 设置表格内容不可编辑
self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.tableWidget.verticalHeader().setHidden(True) # 隐藏行号
self.tableWidget.setRowCount(row) # 根据数据库内容设置表格行
self.tableWidget.setColumnCount(column) # 设置表格列
# 设置表格头部
self.tableWidget.setHorizontalHeaderLabels(['排名', '书名', '京东价', '定价', '出版社'])
self.tableWidget.setStyleSheet("background-color:rgba(0,0,0,0)") # 设置背景透明
# 根据窗体大小拉伸表格
self.tableWidget.horizontalHeader().setSectionResizeMode(
QtWidgets.QHeaderView.ResizeToContents)
for i in range(row):
for j in range(column):
temp_data = results[i][j] # 临时记录,不能直接插入表格
data = QtWidgets.QTableWidgetItem(str(temp_data)) # 转换后可插入表格
self.tableWidget.setItem(i, j, data) # 将信息显示在表格当中
# 设置表格内容文字大小
font = QtGui.QFont()
font.setPointSize(12)
self.tableWidget.setFont(font)
mysql.close_sql() # 提取完数据以后关掉数据库
self.tableWidget.setObjectName("tableWidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 801, 71))
font = QtGui.QFont()
font.setBold(True)
font.setPointSize(12)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "销量排行榜"))
self.label.setText(_translate("MainWindow", "计算机与互联网图书销量排行榜"))