想用更高級界面編程,我們可以嘗試下pyqt5
linux安裝庫方法
sudo apt-get install pyqt5*
sudo apt-get install qt5-default qttools5-dev-tools
爲了方便idea之類便捷調用設計和生成代碼,可以在ide裏 設置 -》工具-》外部工具 增加快捷方式
增加QT設計界面“Qt Designer” — 這個就是設計Qt界面的工具
Program選擇PyQt安裝目錄中 designer.exe 的路徑
parameters設置爲$FileName$
Work directory 使用變量 $ProjectFileDir$
增加“PyUIC” — 這個主要是用來將 Qt界面 轉換成 py代碼
Program選擇PyQt安裝目錄中 pyuic5.bat 的路徑 如果是linux (/usr/bin/pyuic5
parameters設置爲$FileName$ -o $FileNameWithoutExtension$.py
Work directory 設置爲 $FileDir$
用designer新建一個ui文件,然後右鍵選pyuic轉爲py代碼文件,爲了以後修改界面,另外文件寫業務邏輯,繼承此界面即可
win給python3安裝pyqt5方法,linux應該也可用,比apt安裝更好一些
D:\file\python\Scripts>python -m pip install pyqt5 -i https://pypi.douban.com/simple
安裝qt界面設計工具
python -m pip install PyQt5-tools -i https://pypi.douban.com/simple
集成到ide時win沒有專門的pyuic?就用python
參數-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
順版還可以安裝打包工具
pip install pyinstaller -i https://pypi.douban.com/simple
pyinstaller --onefile --windowed mainp.py就可以打包成一個單文件不需要控制檯窗口程序
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(640, 480)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(60, 280, 461, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(190, 100, 181, 29))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 100, 69, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(70, 170, 69, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setGeometry(QtCore.QRect(190, 170, 181, 29))
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(400, 170, 90, 29))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "qtdesigner很方便"))
self.label.setText(_translate("Dialog", "用戶名"))
self.label_2.setText(_translate("Dialog", "密碼"))
self.pushButton.setText(_translate("Dialog", "確認"))
如上是簡單劃了幾下生成一個p1.py界面文件,引導進我們業務邏輯文件內就可以使用了
from PyQt5 import QtCore, QtGui, QtWidgets
from p1 import Ui_Dialog
import sys
class MyQt(Ui_Dialog):
def __init__(self):
pass
def reject(self):
print("reject")
def accept(self):
print("accept")
def setupUi(self, Dialog):
super().setupUi(Dialog) # 繼承父類功能
self.a = 0
self.pushButton.clicked.connect(self.test)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
def test(self):
self.a += 1
print(self.a)
print(self.lineEdit.text())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
ui = MyQt()
ui.setupUi(widget)
widget.show()
sys.exit(app.exec_())
下次修改界面只需更新p1.py文件,是不是分工明確的感覺?
順便說一聲pyqt5支持python3.5+,如果安裝3.4別折騰,還是及早升級匹配的好