1. 程式人生 > >python2.7+PyQt5 製作桌面便籤小程式

python2.7+PyQt5 製作桌面便籤小程式

    初學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安裝pyinstaller

pip install pyinstaller

        3.2pyinstaller常規使用

        3.3開啟命令列視窗,在要打包的程式目錄下,執行以下命令

pyinstaller -F -w main.py

                之後會在dist資料夾下生成main.exe 檔案

                 執行exe檔案即可