前面說到python小工具的開發(fā)是一件非常有意思的事情。可以用PyQt5、PyQt5-tools的包來實(shí)現(xiàn)。但是后來了解到pyside2是python本家開發(fā)的包,以后的使用范圍和生命周期會(huì)更加穩(wěn)定和有保障,故比較推薦改用pyside2進(jìn)行開發(fā)。之前用PyQt5來開發(fā)的同學(xué)不必緊張,目前兩者只有導(dǎo)入包的區(qū)別。改變導(dǎo)入包,即可以輕松從PyQt5轉(zhuǎn)戰(zhàn)Pyside2。
因?yàn)閜yside2是python自己開發(fā)的,未來的使用會(huì)更加廣泛,可以通過安裝pyside2代替PyQt5及PyQt5-tools
安裝方法:pip install pyside2
使用pyside2的安裝命令
pyinstaller stats2.py --noconsole --hidden-import PySide2.QtXml
為ui文件時(shí),在入口py文件對(duì)ui文件動(dòng)態(tài)加載的方法:
from PySide2.QtWidgetsimport QApplication, QMessageBox
from PySide2.QtUiToolsimport QUiLoader
from PySide2.QtCoreimport QFile
class Stats:
def __init__(self):
# 從文件中加載UI定義(固定寫法)
? ? ? ? qfile_stats = QFile("stats.ui")? ? #如果不在入口文件目錄,需要加上路徑如ui/stats.ui
qfile_stats.open(QFile.ReadOnly)
qfile_stats.close()
# 從 UI 定義中動(dòng)態(tài) 創(chuàng)建一個(gè)相應(yīng)的窗口對(duì)象
? ? ? ? # 注意:里面的控件對(duì)象也成為窗口對(duì)象的屬性了
? ? ? ? # 比如self.ui.button , self.ui.textEdit
? ? ? ? self.ui = QUiLoader().load(qfile_stats)
self.ui.button.clicked.connect(self.handleCalc)
——————————————————————————————
中間是定義handleCalc行為自己發(fā)揮啊啊啊啊啊啊
——————————————————————————————
app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()
將ui轉(zhuǎn)換為py文件后需要加的內(nèi)容
import sys #支持下面方法需要導(dǎo)入的包
中間自己寫xxx
if __name__ =="__main__":
app = QtWidgets.QApplication(sys.argv)# 創(chuàng)建一個(gè)QApplication,也就是你要開發(fā)的軟件app
? ? MainWindow = QtWidgets.QMainWindow()# 創(chuàng)建一個(gè)QMainWindow,用來裝載你需要的各種組件、控件
? ? ui = Ui_Form()# ui是你創(chuàng)建的ui類的實(shí)例化對(duì)象
? ? ui.setupUi(MainWindow)# 執(zhí)行類中的setupUi方法,方法的參數(shù)是第二步中創(chuàng)建的QMainWindow
? ? MainWindow.show()# 執(zhí)行QMainWindow的show()方法,顯示這個(gè)QMainWindow
? ? sys.exit(app.exec_())# 使用exit()或者點(diǎn)擊關(guān)閉按鈕退出QApplication
pyside2打包方式
pyinstaller -F -w bindp.py? 注:打出單獨(dú)的exe文件,要用py文件,用ui文件的單獨(dú)可執(zhí)行文件打出后不可用
pyinstaller stats2.py --noconsole --hidden-import PySide2.QtXml 注:可用ui,打出的是帶程序文件的文件夾
目前實(shí)踐成功的代碼包括以下:
1.mysql類文件:
#導(dǎo)入mysql包
import pymysql??
#建立mysql連接
conndev = pymysql.connect(
host=" ",
port= ,
user=' ',
password=' ',
database=" ",
charset=" ",
#定義數(shù)據(jù)庫操作方法
def do1(userId, thirdId, ev):
? ? ? #給查詢語句設(shè)置變量
? ? ? sql =' '
? ? #執(zhí)行查詢語句
? ??try:
????????cursor.execute(sql)
????????conndev.commit()
????????conntest_vpgame.commit()
except:
????????conndev.rollback()
????????conntest_vpgame.rollback()
????????print(cursor.execute(sql))
#關(guān)閉數(shù)據(jù)庫連接
cursor.close()# 關(guān)閉游標(biāo)
conndev.close()# 關(guān)閉連接
conntest_vpgame.close()
2.ui類文件
使用Designer工具繪制ui,使用pyuic轉(zhuǎn)換成py文件后,
#導(dǎo)入資源包
from PySide2.QtCoreimport (QCoreApplication, QMetaObject, QRect, Qt)
from PySide2.QtGuiimport (QFont)
from PySide2.QtWidgetsimport *
#創(chuàng)建類
class Ui_Form(QMainWindow):
def setupUi(self, Form):
if Form.objectName():
Form.setObjectName(u"Form")
Form.resize(848, 469)
self.label_1 = QLabel(Form)
中間為ui轉(zhuǎn)換的代碼行
中間為ui轉(zhuǎn)換的代碼行
中間為ui轉(zhuǎn)換的代碼行
中間為ui轉(zhuǎn)換的代碼行
中間為ui轉(zhuǎn)換的代碼行
如果有下拉列表等控件需要添加元素的,需要在轉(zhuǎn)換代碼后手動(dòng)增加,并在以下結(jié)構(gòu)中完善
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u7405\u740a\u5c0f\u5de5\u5177\u4e4b\u7b2c\u4e09\u65b9\u64cd\u4f5c-yena", None))
最后增加各種按鈕或點(diǎn)擊時(shí)間的交互,如:
self.pushButton.clicked.connect(Form.currentdef)
3.方法類文件
#導(dǎo)入包
import sys
from bind1import Ui_Form
import MySqlData
from PySide2.QtWidgetsimport *
#創(chuàng)建類
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
中間是自己定義的其他方法,方法中可以調(diào)用mysql類的方法進(jìn)行操作的執(zhí)行如MySqlData.unbinddib(userId, ev)
中間是自己定義的其他方法
中間是自己定義的其他方法
中間是自己定義的其他方法
#代碼結(jié)尾,喚起app且一直循環(huán)的獲取窗口時(shí)間
if __name__=="__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())