PyQt5 GUI 開發(fā)的基礎知識
簡介
QT的全稱是"Qt Toolkit",是一個跨平臺的C++圖形用戶界面應用程序開發(fā)框架。
Qt(發(fā)音為"cute")是一個由Qt Company開發(fā)的跨平臺應用程序開發(fā)框架,最初由挪威公司Trolltech于1991年創(chuàng)建。它主要用于開發(fā)圖形用戶界面(GUI)應用程序,但也支持非GUI程序的開發(fā),如控制臺工具和服務器。
第一個PyQt程序
import sys
from PyQt5.QtWidgets import QApplication, QWidget
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle("Hello World") # 設置窗口標題
w.show() # 展示窗口
app.exec_() # 程序進行循環(huán)等待狀態(tài)
最常用的三個功能模塊
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") # 設置窗口標題
btn = QPushButton("按鈕") # 在窗口里面添加控件-QPushButton
btn.setParent(w)
w.show() # 展示窗口
app.exec_() # 程序進行循環(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") # 設置窗口標題
# lab = QLabel("賬號: ", w) # 簡寫方式
lab = QLabel("賬號: ")
lab.setParent(w)
lab.setGeometry(20, 20, 300, 300)
w.show() # 展示窗口
app.exec_() # 程序進行循環(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") # 設置窗口標題
edit = QLineEdit(w)
edit.setPlaceholderText("請輸入賬號")
edit.setGeometry(50, 20, 200, 20)
w.show() # 展示窗口
app.exec_() # 程序進行循環(huán)等待狀態(tài)
調整窗口的大小和位置
w=QWidget() w.resize(300, 300) # 設置窗口大小 w.move(0, 0) # 移動窗口 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()
設置窗口的圖標
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("計算器")
edit = QLineEdit()
edit.setPlaceholderText("請輸入內容")
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("賬號")
edit1 = QLineEdit()
edit1.setPlaceholderText("請輸入賬號")
lab2 = QLabel("密碼")
edit2 = QLineEdit()
edit2.setPlaceholderText("請輸入密碼")
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_()
到此這篇關于PyQt5 GUI 開發(fā)基礎的文章就介紹到這了,更多相關PyQt5 GUI 開發(fā)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
django應用JWT(JSON?Web?Token)實戰(zhàn)教程
在前后端分離的項目中,JWT(JSON?Web?Token)作為一種廣泛使用的身份驗證和授權機制,提供了一種安全、高效的方式來保護RESTful?API,本文詳細介紹了JWT的概念、優(yōu)勢、在Django中的應用步驟和使用方法,是構建安全、高效Web應用的有效指南2024-10-10
python設計微型小說網(wǎng)站(基于Django+Bootstrap框架)
這篇文章主要介紹了python設計微型小說網(wǎng)站(基于Django+Bootstrap框架),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07
詳解win10下pytorch-gpu安裝以及CUDA詳細安裝過程
這篇文章主要介紹了win10下pytorch-gpu安裝以及CUDA詳細安裝過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
利用Python自動監(jiān)控網(wǎng)站并發(fā)送郵件告警的方法
這篇文章介紹的是通過定時執(zhí)行python腳本,可以實現(xiàn)定期批量訪問網(wǎng)站,如果發(fā)現(xiàn)網(wǎng)站打不開,第一時間發(fā)郵件到管理員郵箱進行預警。有需要的可以參考借鑒。2016-08-08
PyCharm 無法 import pandas 程序卡住的解決方式
這篇文章主要介紹了PyCharm 無法 import pandas 程序卡住的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python中使用uv創(chuàng)建環(huán)境及原理舉例詳解
uv是Astral團隊開發(fā)的高性能Python工具,整合包管理、虛擬環(huán)境、Python版本控制等功能,這篇文章主要介紹了Python中使用uv創(chuàng)建環(huán)境及原理的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-06-06
Python pandas軸旋轉stack和unstack的使用說明
這篇文章主要介紹了Python pandas軸旋轉stack和unstack的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python推導表達式進行高效數(shù)據(jù)處理的詳細方法
在 Python編程中,推導表達式是一種簡潔且高效的語法結構,能夠快速生成列表,字典,集合等數(shù)據(jù)結構,下面我們來看看Python如何利用推導表達式進行高效數(shù)據(jù)處理吧2025-06-06

