一、Gui基礎介紹
Graphical User Interface,簡稱GUI,指的是圖形用戶界面。與之對應的是Text-based User Interface,即基於文本的用戶界面。GUI 是人們通過視覺和觸覺接受信息,通過鼠標、鍵盤來輸入信息,從而與計算機系統交互的一種表現形式。
Python 作為一門高級語言,它自帶各種 GUI 庫( PyQt5、Tkinter、Pygame、wxPython 等)。這些庫提供各種組件和方法用於快速地構建 GUI 應用程序,縮短了開發 GUI 應用程序的時間。
二、使用 Qt Designer 設計 UI 界面
Qt Designer 是一款方便易用的可視化 UI 設計工具,被廣泛應用於 PyQt5 的開發中。使用 Qt Designer 設計的 GUI 會生成一個 .ui 文件,可以通過利用 PyQt5 中的工具將其轉換成 Python 代碼。
下面是一個簡單的例子,以 PyQt5 和 Qt Designer 的組合為基礎。
# 引入必要的庫
from PyQt5.QtWidgets import QDialog, QApplication, QMessageBox
from PyQt5.uic import loadUi
import sys
class MyForm(QDialog):
def __init__(self):
super().__init__()
# 加載 UI 界面
loadUi('myform.ui', self)
# 設置按鈕點擊事件
self.pushButton.clicked.connect(self.show_message)
def show_message(self):
# 彈出提示框
QMessageBox.information(self, "Hello World", "Welcome to Python GUI programming")
if __name__ == "__main__":
app = QApplication(sys.argv)
form = MyForm()
form.show()
sys.exit(app.exec_())
通過以上代碼,我們可以實現這樣一個簡單的 GUI 程序。在這裡我們引用了 PyQt5 中的 QDialog、QApplication、QMessageBox,以及 PyQt5 中提供的 loadUi 方法。其中 loadUi 方法用於將 .ui 文件加載並轉換成 Python 代碼。
三、常見的 GUI 組件
PyQt5 中常見的 GUI 組件有 QLineEdit、QComboBox、QCheckBox、QPushButton 等。以下是這些組件的用法示例。
1、QLineEdit
QLineEdit 是一個簡單的單行輸入框。我們可以使用 clear()、setText()、text() 等方法操作它。下面是一個簡單的例子。
from PyQt5.QtWidgets import QLineEdit, QApplication, QWidget, QLabel
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 創建一個輸入框
lineEdit = QLineEdit(self)
lineEdit.move(60, 60)
# 創建一個標籤
label = QLabel(self)
label.move(60, 100)
# 設置按鈕點擊事件
lineEdit.textChanged[str].connect(lambda text: label.setText(text))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QLineEdit 例子')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
2、QComboBox
QComboBox 是一個下拉框。我們可以使用 addItem()、removeItem()、currentText() 等方法操作它,來獲取和設置下拉框中的選項。下面是一個簡單的例子。
from PyQt5.QtWidgets import QComboBox, QApplication, QWidget, QLabel
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 創建一個下拉框
comboBox = QComboBox(self)
comboBox.addItem("選項1")
comboBox.addItem("選項2")
comboBox.move(60, 60)
# 創建一個標籤
label = QLabel(self)
label.move(60, 100)
# 設置按鈕點擊事件
comboBox.activated[str].connect(lambda text: label.setText(text))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QComboBox 例子')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
3、QCheckBox
QCheckBox 是一個複選框。我們可以使用 isChecked()、setChecked() 等方法操作它,來獲取和設置複選框的選中狀態。
from PyQt5.QtWidgets import QCheckBox, QApplication, QWidget, QLabel
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 創建一個複選框
checkBox = QCheckBox("選項", self)
checkBox.move(60, 60)
# 創建一個標籤
label = QLabel(self)
label.move(60, 100)
# 設置按鈕點擊事件
checkBox.stateChanged.connect(lambda: label.setText("已選中" if checkBox.isChecked() else "未選中"))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QCheckBox 例子')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
4、QPushButton
QPushButton 是一個普通按鈕。我們可以使用 setText()、setDisabled()、clicked() 等方法操作它,來改變按鈕的文字和狀態,並添加點擊事件。
from PyQt5.QtWidgets import QPushButton, QApplication, QWidget, QLabel
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 創建一個按鈕
button = QPushButton("按鈕", self)
button.move(60, 60)
# 創建一個標籤
label = QLabel(self)
label.move(60, 100)
# 設置按鈕點擊事件
button.clicked.connect(lambda: label.setText("Hello World!"))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QPushButton 例子')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
四、總結
本文主要介紹了如何在 Python 中快速地開發 GUI 程序,同時詳細介紹了 PyQt5 中常見的 GUI 組件的用法。這種方法可以大大縮短開發時間,使開發人員專註於業務邏輯,提高開發效率。
(完)
原創文章,作者:FDIDX,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/317366.html