Python+PyQt5+MySQL實現(xiàn)天氣管理系統(tǒng)
在本篇博客中,我利用Python語言其編寫界面庫PyQt5,然后通過連接MySQL數(shù)據(jù)庫,實現(xiàn)了一個簡單的天氣管理小系統(tǒng),該系統(tǒng)包含簡單的增刪查改四個主要功能。本文旨在解析實現(xiàn)的程序,能夠讓讀者快速了解PyQt5圖形界面庫,然后可以初步實現(xiàn)這樣一個小的系統(tǒng)程序。
PyQt5簡介
PyQt5本身來自C++的界面庫Qt,經(jīng)過一系列的封裝移植到Python里面,作為Python的一個圖像界面庫,它繼承了Python語言簡單易實現(xiàn)的特點(diǎn),可以實現(xiàn)基本的界面效果。里面有許多類實現(xiàn)了我們想要的窗體、表格、文本、圖像等功能。在這個項目中也有所涉及,博主也是初次學(xué)這個庫,然后寫了這個小項目,里面可能會有一些不合適的地方,望諒解。
天氣系統(tǒng)數(shù)據(jù)庫
我將天氣系統(tǒng)數(shù)據(jù)存入MySQL數(shù)據(jù)庫中,提取數(shù)據(jù)時用Python的pymysql庫連接MySQL數(shù)據(jù)庫,對數(shù)據(jù)庫進(jìn)行一系列操作。
這個數(shù)據(jù)庫主要包含城市、時間、各個空氣物質(zhì)的含量、pm2.5、AQI指標(biāo)等。如果需要數(shù)據(jù)可以在下面留言,我可以發(fā)給你們。
實現(xiàn)步驟
- 導(dǎo)入所需要用的Python包:PyQt5,pymysql……沒有的可以直接用pip安裝
- 創(chuàng)建所要編寫的界面類,初始化界面
- 連接數(shù)據(jù)庫,獲取數(shù)據(jù)
- 建立表格、按鈕布局
- 實現(xiàn)功能函數(shù)
- 測試
具體實現(xiàn)過程
#導(dǎo)入包 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) #設(shè)置界面大小、名稱、背景 self.resize(1000,800) self.setWindowTitle('Database') self.setStyleSheet("background-image:url(tubiao_meitu.jpg)") #窗體屬性 self.setWindowFlags(Qt.Widget) #連接數(shù)據(jù)庫 db = pymysql.connect("localhost", "root", "password", "mysql",charset='utf8') #獲取游標(biāo)、數(shù)據(jù) cur = db.cursor() cur.execute("SELECT * FROM pm_25") data = cur.fetchall() #數(shù)據(jù)列名 col_lst = [tup[0] for tup in cur.description] #數(shù)據(jù)的大小 row = len(data) vol = len(data[0]) #插入表格 self.MyTable = QTableWidget(row,vol) font = QtGui.QFont('微軟雅黑',10) #設(shè)置字體、表頭 self.MyTable.horizontalHeader().setFont(font) self.MyTable.setHorizontalHeaderLabels(col_lst) #設(shè)置豎直方向表頭不可見 self.MyTable.verticalHeader().setVisible(False) self.MyTable.setFrameShape(QFrame.NoFrame) #設(shè)置表格顏色 self.MyTable.horizontalHeader().setStyleSheet('QHeaderView::section{background:skyblue}') #構(gòu)建表格插入數(shù)據(jù) for i in range(row): for j in range(vol): temp_data = data[i][j] # 臨時記錄,不能直接插入表格 data1 = QTableWidgetItem(str(temp_data)) # 轉(zhuǎn)換后可插入表格 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) #設(shè)置按鈕內(nèi)字體樣式 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))#插入實現(xiàn) okButton.clicked.connect(partial(self.up_data, cur, db,col_lst))#插入實現(xiàn) deleteButton.clicked.connect(partial(self.del_data,cur,db))#刪除實現(xiàn) inquireButton.clicked.connect(partial(self.inq_data,db))#查詢實現(xiàn) #添加空表格 def add_data(self,cur,db): #獲取行數(shù) row = self.MyTable.rowCount() #在末尾插入一空行 self.MyTable.insertRow(row) #插入數(shù)據(jù) 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: #當(dāng)前行 row_2 = self.MyTable.currentRow() del_d = self.MyTable.item(row_2, 0).text() #在數(shù)據(jù)庫刪除數(shù)據(jù) 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) #查詢到的更新帶表格當(dāng)中 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)) # 轉(zhuǎn)換后可插入表格 self.MyTable.setItem(i_x, j_y, data_1) #空輸入返回原先數(shù)據(jù)表格 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)) # 轉(zhuǎn)換后可插入表格 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的基本使用方法和數(shù)據(jù)庫的基本語法,做起來還是比較得心應(yīng)手的。
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python GUI庫圖形界面開發(fā)之PyQt5窗口類QMainWindow詳細(xì)使用方法
- python GUI庫圖形界面開發(fā)之PyQt5中QMainWindow, QWidget以及QDialog的區(qū)別和選擇
- Python PyQt5-圖形界面的美化操作
- python中pyqtgraph知識點(diǎn)總結(jié)
- 詳解Python GUI編程之PyQt5入門到實戰(zhàn)
- 詳解Python3.8+PyQt5+pyqt5-tools+Pycharm配置詳細(xì)教程
- Python3.7安裝PyQt5 運(yùn)行配置Pycharm的詳細(xì)教程
- python3.6.8 + pycharm + PyQt5 環(huán)境搭建的圖文教程
- Python+PyQt5實現(xiàn)滅霸響指功能
- Python3.7下安裝pyqt5的方法步驟(圖文)
- Python PyQt5運(yùn)行程序把輸出信息展示到GUI圖形界面上
- PyQt QMainWindow的使用示例
相關(guān)文章
關(guān)于Python正則表達(dá)式模塊之re模塊
這篇文章主要介紹了關(guān)于Python正則表達(dá)式模塊之re模塊,?re模塊是Python中的重要組成部分,這里涉及到字符串的匹配,轉(zhuǎn)換,自定義格式化等,需要的朋友可以參考下2023-04-04基于python實現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于python學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11Python數(shù)據(jù)結(jié)構(gòu)鏈表操作從基礎(chǔ)到高級實例深究
鏈表是一種基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu),它由一系列節(jié)點(diǎn)組成,每個節(jié)點(diǎn)都包含數(shù)據(jù)和指向下一個節(jié)點(diǎn)的引用,在Python中,可以使用類來實現(xiàn)鏈表,本文將介紹如何實現(xiàn)鏈表,并提供一些豐富的示例代碼來幫助你更好地理解其原理和應(yīng)用2023-12-12python使用pandas實現(xiàn)數(shù)據(jù)分割實例代碼
這篇文章主要介紹了python使用pandas實現(xiàn)數(shù)據(jù)分割實例代碼,介紹了使用pandas實現(xiàn)對dataframe格式的數(shù)據(jù)分割成時間跨度相等的數(shù)據(jù)塊,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01python獲取當(dāng)前文件所在目錄、獲取上級目錄的坑及解決
這篇文章主要介紹了python獲取當(dāng)前文件所在目錄、獲取上級目錄的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Perl中著名的Schwartzian轉(zhuǎn)換問題解決實現(xiàn)
這篇文章主要介紹了Perl中著名的Schwartzian轉(zhuǎn)換問題解決實現(xiàn),本文詳解講解了Schwartzian轉(zhuǎn)換涉及的排序問題,并同時給出實現(xiàn)代碼,需要的朋友可以參考下2015-06-06spyder 在控制臺(console)執(zhí)行python文件,debug python程序方式
這篇文章主要介紹了spyder 在控制臺(console)執(zhí)行python文件,debug python程序方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python使用Beautiful?Soup(BS4)庫解析HTML和XML
這篇文章介紹了Python使用Beautiful?Soup(BS4)庫解析HTML和XML的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06