1. 程式人生 > >Pyqt5系列(二 )-第一個PyQt程式

Pyqt5系列(二 )-第一個PyQt程式

通過下面的一個PyQt5簡單例子,來簡單瞭解一下關於如何建立PyQt5的。具體程式碼如下:
#-*- coding:utf-8 -*-
'''
Frist PyQt5 program
'''
__author__ = 'Tony Zhu'

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QPushButton, QLineEdit, QVBoxLayout,QMessageBox
import sys

class ShowWindow(QWidget):

    def __init__
(self):
super(ShowWindow,self).__init__() self.initUI() def initUI(self): self.inputLabel = QLabel("Input your text") self.editLine = QLineEdit() self.printButton = QPushButton("Print") self.clearButton = QPushButton("Clear") self.printButton.clicked.connect(self.printText) self.clearButton.clicked.connect(self.clearText) inputLayout = QHBoxLayout() inputLayout.addWidget(self.inputLabel) inputLayout.addWidget(self.editLine) buttonLayout = QHBoxLayout() buttonLayout.addWidget(self.printButton) buttonLayout.addWidget(self.clearButton) mainLayout = QVBoxLayout() mainLayout.addLayout(inputLayout) mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) self.setWindowTitle('FristWindow'
) self.show() def printText(self): text = self.editLine.text() if text == '': QMessageBox.information(self, "Empty Text", "Please enter the letter.") else : QMessageBox.information(self, "Print Success"
, "Text: %s" % text) def clearText(self): text = self.editLine.text() if text == '': return else : self.editLine.clear() if __name__ == '__main__': app = QApplication(sys.argv) ex = ShowWindow() sys.exit(app.exec_())

執行的結果:
程式執行後的結果

結合程式碼和執行的結果,分析程式碼:
Line7~8:

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QPushButton, QLineEdit, QVBoxLayout,QMessageBox
import sys

匯入了程式執行所必須的模組

Line10:

class ShowWindow(QWidget):

建立一個ShowWindow類繼承QWidget,其中PyQt中非常重要的通用視窗類,是所有使用者介面物件的基類,所有和使用者介面相關的控制元件類都是繼承自QWidger類。

Line12~14:

def __init__(self):
        super(ShowWindow,self).__init__()      
        self.initUI()

定義了ShowWindow類的建構函式 init,由於ShowWindow繼承自QWidgert類,因此在建構函式中呼叫父類QWidget的建構函式super.init()。
同時在建構函式中呼叫自定義的初始化函式initUI(),在該函式中初始化GUI中所需各個控制元件。

Line17~20:

self.inputLabel = QLabel("Input your text")
self.editLine = QLineEdit()
self.printButton = QPushButton("Print")
self.clearButton = QPushButton("Clear")

建立成員:一個標籤(inputLabel ),輸入框(editLine ),兩個按鈕(printButton ,clearButton )

Line22~23:

self.printButton.clicked.connect(self.printText)        self.clearButton.clicked.connect(self.clearText)

printButton和clearButton點選事件處理的邏輯:點選printButton之後會呼叫自定義函式printText()中的處理邏輯;點選clearButton之後呼叫自定義函式clearText()中的處理邏輯。通過connect()方法將點選事件和處理邏輯關聯起來
這個涉及到PyQt訊號與槽的概念,訊號和槽用於物件之間的通訊。當指定事件發生,一個事件訊號會被髮射。槽可以被任何Python指令碼呼叫。當和槽連線的訊號被髮射時,槽會被呼叫。有關訊號與槽的概念,在後續的文章中會專門講解。

Line25~26:

inputLayout = QHBoxLayout()
inputLayout.addWidget(self.inputLabel)
inputLayout.addWidget(self.editLine)

建立一個inputLayout的水平佈局(QHBoxLayout),在inputLayout中新增已定義的控制元件inputLabel和editLine。QHBoxLayout讓新增的控制元件水平排布

Line29~31:

buttonLayout = QHBoxLayout()
buttonLayout.addWidget(self.printButton)
buttonLayout.addWidget(self.clearButton)

同上建立一個buttonLayout 的水平佈局(QHBoxLayout),在buttonLayout 中新增printButton和clearButton

Line33~35:

mainLayout = QVBoxLayout()
mainLayout.addLayout(inputLayout)
mainLayout.addLayout(buttonLayout)

建立一個mainLayout 的垂直佈局(QVBoxLayout),在mainLayout 中巢狀子佈局inputLayout和buttonLayout。

通過如下的佈局圖,進一步瞭解一下該程式中layout是如何佈局的。
layout

有layout的概念,在後續的文章中會專門講解。

Line37:

self.setLayout(mainLayout)

通過將setLayout()方法,將mainLayout設定為視窗layout。

Line38:

self.setWindowTitle('FristWindow') 

通過setWindowTitle()方法設定視窗的標題

Line39:

self.show()

通過show()方法將視窗顯示到螢幕上

Line40~48:

    def printText(self):
        text = self.editLine.text()

        if text == '':
            QMessageBox.information(self, "Empty Text",
                                    "Please enter the letter.")
        else :
            QMessageBox.information(self, "Print Success",
                                    "Text: %s" % text)

定義了處理printButton點選訊號的槽函式,當editLine輸入框中的文字為空的時候彈出訊息框(QMessageBox),提示“Please enter the letter.”;當editLine輸入框中的文字不為空時候彈出訊息框,顯示editLine中的文字。

觸發槽函式printText()之後,當editLine輸入框內容為空時介面顯示如下:
MessageBox

Line49~55:

    def clearText(self):
        text = self.editLine.text()

        if text == '':
            return
        else :
            self.editLine.clear()

定義了處理clearButton點選訊號的槽函式,清空editLine輸入框中的文字資訊。

Line 57~60:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = ShowWindow()
    sys.exit(app.exec_())

程式執行的入口函式類似C語言中的main()方法。
app = QApplication(sys.argv),每一個pyqt程式必須建立一個application物件,sys.argv是命令列引數,可以通過命令啟動的時候傳遞引數。
ex = ShowWindow(),建立ShowWindow物件。
sys.exit(app.exec_()),app.exec_() 事件處理開始,進入程式主迴圈。主迴圈等待時間,並把事件傳送給指定處理的任務中。當呼叫app.exit()或者程式因為各種原因被破壞後,使用sys.exit()關閉程式,並釋放記憶體資源。

到此為止,第一個PyQt程式分析結束,可能會存在輸入框,佈局,等各類控制元件如何使用,如何確定對應的控制元件在那個模組中?在後續的文章中會詳細進行介紹。