在本篇博客中,我利用Python語言其編寫界面庫PyQt5,然后通過連接MySQL數據庫,實現了一個簡單的天氣管理小系統,該系統包含簡單的增刪查改四個主要功能。本文旨在解析實現的程序,能夠讓讀者快速了解PyQt5圖形界面庫,然后可以初步實現這樣一個小的系統程序。
PyQt5簡介
PyQt5本身來自C++的界面庫Qt,經過一系列的封裝移植到Python里面,作為Python的一個圖像界面庫,它繼承了Python語言簡單易實現的特點,可以實現基本的界面效果。里面有許多類實現了我們想要的窗體、表格、文本、圖像等功能。在這個項目中也有所涉及,博主也是初次學這個庫,然后寫了這個小項目,里面可能會有一些不合適的地方,望諒解。
天氣系統數據庫
我將天氣系統數據存入MySQL數據庫中,提取數據時用Python的pymysql庫連接MySQL數據庫,對數據庫進行一系列操作。
這個數據庫主要包含城市、時間、各個空氣物質的含量、pm2.5、AQI指標等。如果需要數據可以在下面留言,我可以發給你們。
實現步驟
- 導入所需要用的Python包:PyQt5,pymysql……沒有的可以直接用pip安裝
- 創建所要編寫的界面類,初始化界面
- 連接數據庫,獲取數據
- 建立表格、按鈕布局
- 實現功能函數
- 測試
具體實現過程
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#導入包 import pymysql from functools import partial from PyQt5.Qt import QWidget from PyQt5 import QtGui,QtWidgets from PyQt5.QtCore import Qt from PyQt5.QtWidgets import (QFrame,QApplication,QDialog, QDialogButtonBox, QMessageBox,QVBoxLayout, QLineEdit,QTableWidgetItem,QTableWidget,QHBoxLayout) #建立界面類 class creat_view(QDialog): def __init__( self ,parent = None ): super (creat_view, self ).__init__(parent) #設置界面大小、名稱、背景 self .resize( 1000 , 800 ) self .setWindowTitle( 'Database' ) self .setStyleSheet( "background-image:url(tubiao_meitu.jpg)" ) #窗體屬性 self .setWindowFlags(Qt.Widget) #連接數據庫 db = pymysql.connect( "localhost" , "root" , "password" , "mysql" ,charset = 'utf8' ) #獲取游標、數據 cur = db.cursor() cur.execute( "SELECT * FROM pm_25" ) data = cur.fetchall() #數據列名 col_lst = [tup[ 0 ] for tup in cur.description] #數據的大小 row = len (data) vol = len (data[ 0 ]) #插入表格 self .MyTable = QTableWidget(row,vol) font = QtGui.QFont( '微軟雅黑' , 10 ) #設置字體、表頭 self .MyTable.horizontalHeader().setFont(font) self .MyTable.setHorizontalHeaderLabels(col_lst) #設置豎直方向表頭不可見 self .MyTable.verticalHeader().setVisible( False ) self .MyTable.setFrameShape(QFrame.NoFrame) #設置表格顏色 self.MyTable.horizontalHeader().setStyleSheet('QHeaderView::section{background:skyblue}') #構建表格插入數據 for i in range (row): for j in range (vol): temp_data = data[i][j] # 臨時記錄,不能直接插入表格 data1 = QTableWidgetItem( str (temp_data)) # 轉換后可插入表格 self .MyTable.setItem(i, j, data1) #編輯按鈕 self .qle = QLineEdit() buttonBox = QDialogButtonBox() #增刪查改四個按鈕 addButton = buttonBox.addButton( "&ADD" ,QDialogButtonBox.ActionRole) okButton = buttonBox.addButton( "&OK" ,QDialogButtonBox.ActionRole) deleteButton = buttonBox.addButton( "&DELETE" ,QDialogButtonBox.ActionRole) inquireButton = buttonBox.addButton( "&QUERY" ,QDialogButtonBox.ActionRole) #設置按鈕內字體樣式 addButton.setFont(font) okButton.setFont(font) deleteButton.setFont(font) inquireButton.setFont(font) #垂直布局 layout = QVBoxLayout() layout.addWidget( self .qle) layout.addWidget(buttonBox) layout.addWidget( self .MyTable) self .setLayout(layout) addButton.clicked.connect(partial( self .add_data,cur,db)) #插入實現 okButton.clicked.connect(partial( self .up_data, cur, db,col_lst)) #插入實現 deleteButton.clicked.connect(partial( self .del_data,cur,db)) #刪除實現 inquireButton.clicked.connect(partial( self .inq_data,db)) #查詢實現 #添加空表格 def add_data( self ,cur,db): #獲取行數 row = self .MyTable.rowCount() #在末尾插入一空行 self .MyTable.insertRow(row) #插入數據 def up_data( self ,cur,db,col_lst): row_1 = self .MyTable.rowCount() value_lst = [] for i in range ( len (col_lst)): if ( len ( self .MyTable.item(row_1 - 1 ,i).text()) = = 0 ): value_lst.append( None ) else : value_lst.append( self .MyTable.item(row_1 - 1 ,i).text()) tup_va_lst = [] for cl,va in zip (col_lst,value_lst): tup_va_lst.append((cl,va)) #插入語句 cur.execute( "INSERT INTO pm_25 VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" ,value_lst) #刪除 def del_data( self ,cur,db): #是否刪除的對話框 reply = QMessageBox.question( self , 'Message' , 'Are you sure to delete it ?' , QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply = = QMessageBox.Yes: #當前行 row_2 = self .MyTable.currentRow() del_d = self .MyTable.item(row_2, 0 ).text() #在數據庫刪除數據 cur.execute( "DELETE FROM pm_25 WHERE f_id = '" + del_d + "'" ) db.commit() #刪除表格 self .MyTable.removeRow(row_2) #查詢 def inq_data( self ,db): txt = self .qle.text() #模糊查詢 if len (txt) ! = 0 : cur.execute( "SELECT * FROM pm25_fn WHERE f_area LIKE '%" + txt + "%' or f_place LIKE '%" + txt + "%'" ) # CONCAT('f_id','f_area','f_place','f_AQI','f_AQItype','f_PM25per1h'),concat(concat('%','#txt'),'%') data_x = cur.fetchall() self .MyTable.clearContents() row_4 = len (data_x) vol_1 = len (cur.description) #查詢到的更新帶表格當中 for i_x in range (row_4): for j_y in range (vol_1): temp_data_1 = data_x[i_x][j_y] # 臨時記錄,不能直接插入表格 data_1 = QTableWidgetItem( str (temp_data_1)) # 轉換后可插入表格 self .MyTable.setItem(i_x, j_y, data_1) #空輸入返回原先數據表格 else : self .MyTable.clearContents() cur.execute( "SELECT * FROM pm_25" ) data_y = cur.fetchall() row_5 = len (data_y) vol_1 = len (cur.description) for i_x_1 in range (row_5): for j_y_1 in range (vol_1): temp_data_2 = data_y[i_x_1][j_y_1] # 臨時記錄,不能直接插入表格 data_2 = QTableWidgetItem( str (temp_data_2)) # 轉換后可插入表格 self .MyTable.setItem(i_x_1, j_y_1, data_2) def main(): #顯示 app = QApplication(sys.argv) c = creat_view() c.show() sys.exit(app.exec_()) main() |
界面展示
大致就這么多啦,只要掌握PyQt的基本使用方法和數據庫的基本語法,做起來還是比較得心應手的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_40707407/article/details/81814482