PyQt5 GUI 開發(fā)的基礎(chǔ)知識(shí)
簡介
QT的全稱是"Qt Toolkit",是一個(gè)跨平臺(tái)的C++圖形用戶界面應(yīng)用程序開發(fā)框架。
Qt(發(fā)音為"cute")是一個(gè)由Qt Company開發(fā)的跨平臺(tái)應(yīng)用程序開發(fā)框架,最初由挪威公司Trolltech于1991年創(chuàng)建。它主要用于開發(fā)圖形用戶界面(GUI)應(yīng)用程序,但也支持非GUI程序的開發(fā),如控制臺(tái)工具和服務(wù)器。
第一個(gè)PyQt程序
import sys
from PyQt5.QtWidgets import QApplication, QWidget
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle("Hello World") # 設(shè)置窗口標(biāo)題
w.show() # 展示窗口
app.exec_() # 程序進(jìn)行循環(huán)等待狀態(tài)
最常用的三個(gè)功能模塊
QtCore、QtGui、QtWidgets
控件QPushButton(按鈕)
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle("Hello World") # 設(shè)置窗口標(biāo)題
btn = QPushButton("按鈕") # 在窗口里面添加控件-QPushButton
btn.setParent(w)
w.show() # 展示窗口
app.exec_() # 程序進(jìn)行循環(huán)等待狀態(tài)
控件QLable(純文本)
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle("Hello World") # 設(shè)置窗口標(biāo)題
# lab = QLabel("賬號(hào): ", w) # 簡寫方式
lab = QLabel("賬號(hào): ")
lab.setParent(w)
lab.setGeometry(20, 20, 300, 300)
w.show() # 展示窗口
app.exec_() # 程序進(jìn)行循環(huán)等待狀態(tài)
控件QLineEdit(文本框)
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle("Hello World") # 設(shè)置窗口標(biāo)題
edit = QLineEdit(w)
edit.setPlaceholderText("請(qǐng)輸入賬號(hào)")
edit.setGeometry(50, 20, 200, 20)
w.show() # 展示窗口
app.exec_() # 程序進(jìn)行循環(huán)等待狀態(tài)
調(diào)整窗口的大小和位置
w=QWidget() w.resize(300, 300) # 設(shè)置窗口大小 w.move(0, 0) # 移動(dòng)窗口 desktop = QDesktopWidget() center_point = desktop.availableGeometry().center() x = center_point.x() y = center_point.y() print(w.frameGeometry()) print(w.frameGeometry().getRect()) old_x, old_y, fg_w, fg_h = w.frameGeometry().getRect()
設(shè)置窗口的圖標(biāo)
w = QWidget()
w.setWindowTitle("Hello World")
icon = QIcon("windows_icon.png")
w.setWindowIcon(icon)
布局-QBoxLayout
垂直布局-QVBoxLayout
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class MyWindow(QWidget):
""""""
def __init__(self):
""""""
super().__init__()
self.setWindowTitle("垂直布局")
self.icon = QIcon("windows_icon.png")
self.setWindowIcon(self.icon)
self.resize(500, 400)
self.layout = QVBoxLayout()
self.button_layout()
self.setLayout(self.layout)
def button_layout(self):
""""""
btn1 = QPushButton("按鈕1")
self.layout.addWidget(btn1)
btn2 = QPushButton("按鈕2")
self.layout.addWidget(btn2)
btn3 = QPushButton("按鈕3")
self.layout.addWidget(btn3)
self.layout.addStretch()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec_()
水平布局-QHBoxLayout
""""""
import sys
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QRadioButton,
QVBoxLayout,
QHBoxLayout,
QGroupBox,
)
class MyWindow(QWidget):
""""""
def __init__(self):
""""""
super().__init__()
self.setWindowTitle("Hello World")
self.setup()
def setup(self):
""""""
hobby_box = self.generate_hobby_group_box()
gender_box = self.generate_gender_group_box()
container = QVBoxLayout()
container.addWidget(hobby_box)
container.addWidget(gender_box)
self.setLayout(container)
@staticmethod
def generate_hobby_group_box():
""""""
hobby_box = QGroupBox("愛好")
hobby_layout = QVBoxLayout()
btn1 = QRadioButton("抽煙")
btn2 = QRadioButton("打牌")
hobby_layout.addWidget(btn1)
hobby_layout.addWidget(btn2)
hobby_box.setLayout(hobby_layout)
return hobby_box
@staticmethod
def generate_gender_group_box():
""""""
gender_box = QGroupBox("性別")
gender_layout = QHBoxLayout()
btn1 = QRadioButton("男")
btn2 = QRadioButton("女")
gender_layout.addWidget(btn1)
gender_layout.addWidget(btn2)
gender_box.setLayout(gender_layout)
return gender_box
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec_()
布局-QGridLayout
import sys
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QLineEdit,
QVBoxLayout,
QGridLayout,
QPushButton,
)
class MyWindow(QWidget):
""""""
def __init__(self):
""""""
super().__init__()
self.setup()
def setup(self):
""""""
self.setWindowTitle("計(jì)算器")
edit = QLineEdit()
edit.setPlaceholderText("請(qǐng)輸入內(nèi)容")
edit.setParent(self)
grid_layout = self.generate_grid_layout()
container = QVBoxLayout()
container.addWidget(edit)
container.addLayout(grid_layout)
self.setLayout(container)
@staticmethod
def generate_grid_layout():
""""""
data = {
0: ["%", "CE", "C", "<-"],
1: ["7", "8", "9", "/"],
2: ["4", "5", "6", "x"],
3: ["1", "2", "3", "-"],
4: ["0", ".", "=", "+"],
}
grid_layout = QGridLayout()
for row, row_values in data.items():
for col, value in enumerate(row_values):
btn = QPushButton(value)
grid_layout.addWidget(btn, row, col)
return grid_layout
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec_()
布局-QFormLayout
import sys
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QFormLayout,
QPushButton,
QLineEdit,
QLabel,
)
class MyWindow(QWidget):
""""""
def __init__(self):
""""""
super().__init__()
self.setWindowTitle("Hello World")
form_layout = QFormLayout()
lab1 = QLabel("賬號(hào)")
edit1 = QLineEdit()
edit1.setPlaceholderText("請(qǐng)輸入賬號(hào)")
lab2 = QLabel("密碼")
edit2 = QLineEdit()
edit2.setPlaceholderText("請(qǐng)輸入密碼")
form_layout.addRow(lab1, edit1)
form_layout.addRow(lab2, edit2)
btn = QPushButton("登錄")
container = QVBoxLayout()
container.addLayout(form_layout)
container.addWidget(btn)
self.setLayout(container)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec_()
到此這篇關(guān)于PyQt5 GUI 開發(fā)基礎(chǔ)的文章就介紹到這了,更多相關(guān)PyQt5 GUI 開發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python實(shí)現(xiàn)為PDF文檔設(shè)置和移除密碼
在數(shù)字化時(shí)代,文檔的安全性變得越來越重要,特別是對(duì)于包含敏感信息的PDF文件,所以本文主要來和大家介紹一下如何使用Python實(shí)現(xiàn)為PDF文檔設(shè)置和移除密碼,需要的可以參考下2024-03-03
如何利用Python獲取文本中的電話號(hào)碼實(shí)例代碼
Python的文本處理是經(jīng)常碰到的一個(gè)問題,下面這篇文章主要給大家介紹了關(guān)于如何利用Python獲取文本中的電話號(hào)碼的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Python下載網(wǎng)絡(luò)文本數(shù)據(jù)到本地內(nèi)存的四種實(shí)現(xiàn)方法示例
這篇文章主要介紹了Python下載網(wǎng)絡(luò)文本數(shù)據(jù)到本地內(nèi)存的四種實(shí)現(xiàn)方法,涉及Python網(wǎng)絡(luò)傳輸、文本讀寫、內(nèi)存I/O、矩陣運(yùn)算等相關(guān)操作技巧,代碼中包含了較為詳盡的注釋說明便于理解,需要的朋友可以參考下2018-02-02
基于Python+PyQt5實(shí)現(xiàn)串口數(shù)據(jù)采集和顯示
文章介紹了如何使用Python和PyQt5實(shí)現(xiàn)串口數(shù)據(jù)采集和實(shí)時(shí)通信,首先,文章詳細(xì)描述了環(huán)境搭建步驟,包括Python和PyQt5的安裝和配置,文章詳細(xì)說明了程序?qū)崿F(xiàn)的各個(gè)步驟,包括串口設(shè)置、數(shù)據(jù)讀取、數(shù)據(jù)發(fā)送和報(bào)文解析等,需要的朋友可以參考下2025-05-05
keras實(shí)現(xiàn)VGG16方式(預(yù)測一張圖片)
這篇文章主要介紹了keras實(shí)現(xiàn)VGG16方式(預(yù)測一張圖片),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
解決Python中的ModuleNotFoundError:?No?module?named?'pad
你是否在嘗試導(dǎo)入`paddle`模塊時(shí)遇到了"ModuleNotFoundError:?No?module?named?'paddle'"這個(gè)錯(cuò)誤?別擔(dān)心,我們的指南會(huì)告訴你如何解決,這就像找到丟失的鑰匙一樣簡單,讓我們一起來看看如何解決這個(gè)問題吧!2024-03-03
深入理解Python 關(guān)于supper 的 用法和原理
這篇文章主要介紹了Python 關(guān)于supper 的 用法和原理分析,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-02-02

