初学PyQt5,尝试制作了如图所示桌面便签小程序,功能比较单一。
现按以下顺序记录:
一.PyQt5的安装
二.程序
三.打包
######################################################################
一.PyQt5的安装
二.程序
1.使用Qt Creator 进行排版并设置链接
2.程序
2.1 将ui文件转换成py文件以便主程序调用
在该文件夹下打开终端,输入pyuic5 form.ui -o form.py 2.2 主程序
from form import Ui_Form
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox
class Time_table(QWidget, Ui_Form):
def __init__(self):
super(Time_table,self).__init__()
self.setupUi(self)
self.setWindowTitle('Time Table')
def submit_1(self):
self.lineEdit.text()
self.sb1.clicked.connect(self.ban_1)
self.f1.clicked.connect(self.f_1)
self.f1.clicked.connect(self.message)
def submit_2(self):
self.lineEdit_2.text()
self.sb2.clicked.connect(self.ban_2)
self.f2.clicked.connect(self.f_2)
self.f2.clicked.connect(self.message)
def submit_3(self):
self.lineEdit_3.text()
self.sb3.clicked.connect(self.ban_3)
self.f3.clicked.connect(self.f_3)
self.f3.clicked.connect(self.message)
def submit_4(self):
self.lineEdit_4.text()
self.sb4.clicked.connect(self.ban_4)
self.f4.clicked.connect(self.f_4)
self.f4.clicked.connect(self.message)
def submit_5(self):
self.lineEdit_5.text()
self.sb5.clicked.connect(self.ban_5)
self.f5.clicked.connect(self.f_5)
self.f5.clicked.connect(self.message)
def f_1(self):
self.lineEdit.setEnabled(True)
def f_2(self):
self.lineEdit_2.setEnabled(True)
def f_3(self):
self.lineEdit_3.setEnabled(True)
def f_4(self):
self.lineEdit_4.setEnabled(True)
def f_5(self):
self.lineEdit_5.setEnabled(True)
def ban_1(self):
self.lineEdit.setEnabled(False)
def ban_2(self):
self.lineEdit_2.setEnabled(False)
def ban_3(self):
self.lineEdit_3.setEnabled(False)
def ban_4(self):
self.lineEdit_4.setEnabled(False)
def ban_5(self):
self.lineEdit_5.setEnabled(False)
def message(self):
QMessageBox.information(self,'Tips','Good Job!')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Time_table()
window.show()
sys.exit(app.exec_())
接下来分别解释:from form3 import Ui_Form3
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox
调用相关模块class Time_table(QWidget, Ui_Form):
def __init__(self):
super(Time_table,self).__init__() #py3里面只需super().__init__()self.setupUi(self)
self.setWindowTitle('Time Table')
注意:要把Ui_Form加入类中def submit_1(self):
self.lineEdit.text()
self.sb1.clicked.connect(self.ban_1)
self.f1.clicked.connect(self.f_1)
self.f1.clicked.connect(self.message)
def submit_2(self):
self.lineEdit_2.text()
self.sb2.clicked.connect(self.ban_2)
self.f2.clicked.connect(self.f_2)
self.f2.clicked.connect(self.message)
def submit_3(self):
self.lineEdit_3.text()
self.sb3.clicked.connect(self.ban_3)
self.f3.clicked.connect(self.f_3)
self.f3.clicked.connect(self.message)
def submit_4(self):
self.lineEdit_二.4.text()
self.sb4.clicked.connect(self.ban_4)
se二.lf.f4.clicked.connect(self.f_4)
self.f4.clicked.connect(self.message)
def submit_5(self):
self.lineEdit_5.text()
self.sb5.clicked.connect(self.ban_5)
self.f5.clicked.connect(self.f_5)
self.f5.clicked.connect(self.message)
text内容填写操作,按钮submit和finish操作链接def f_1(self):
self.lineEdit.setEnabled(True)
def f_2(self):
self.lineEdit_2.setEnabled(True)
def f_3(self):
self.lineEdit_3.setEnabled(True)
def f_4(self):
self.lineEdit_4.setEnabled(True)
def f_5(self):
self.lineEdit_5.setEnabled(True)
def ban_1(self):
self.lineEdit.setEnabled(False)
def ban_2(self):
self.lineEdit_2.setEnabled(False)
def ban_3(self):
self.lineEdit_3.setEnabled(False)
def ban_4(self):
self.lineEdit_4.setEnabled(False)
def ban_5(self):
self.lineEdit_5.setEnabled(False)
text内容的enabled状态。若提交则text内容不可更改,若finish,则可更改。def message(self):
QMessageBox.information(self,'Tips','Good Job!')
若finish,弹出窗口“Good Job!”if __name__ == '__main__':
app = QApplication(sys.argv)
window = Time_table()
window.show()
sys.exit(app.exec_())
常规操作
3.打包成exe文档
3.1安装pyinstallerpip install pyinstaller
3.2pyinstaller常规使用
参考第4部分点击打开链接
3.3打开命令行窗口,在要打包的程序目录下,运行以下命令pyinstaller -F -w main.py
之后会在dist文件夹下生成main.exe 文件
运行exe文件即可
(下载链接:点击打开链接)