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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python下載網(wǎng)絡文本數(shù)據(jù)到本地內存的四種實現(xiàn)方法示例
這篇文章主要介紹了Python下載網(wǎng)絡文本數(shù)據(jù)到本地內存的四種實現(xiàn)方法,涉及Python網(wǎng)絡傳輸、文本讀寫、內存I/O、矩陣運算等相關操作技巧,代碼中包含了較為詳盡的注釋說明便于理解,需要的朋友可以參考下2018-02-02基于Python+PyQt5實現(xiàn)串口數(shù)據(jù)采集和顯示
文章介紹了如何使用Python和PyQt5實現(xiàn)串口數(shù)據(jù)采集和實時通信,首先,文章詳細描述了環(huán)境搭建步驟,包括Python和PyQt5的安裝和配置,文章詳細說明了程序實現(xiàn)的各個步驟,包括串口設置、數(shù)據(jù)讀取、數(shù)據(jù)發(fā)送和報文解析等,需要的朋友可以參考下2025-05-05解決Python中的ModuleNotFoundError:?No?module?named?'pad
你是否在嘗試導入`paddle`模塊時遇到了"ModuleNotFoundError:?No?module?named?'paddle'"這個錯誤?別擔心,我們的指南會告訴你如何解決,這就像找到丟失的鑰匙一樣簡單,讓我們一起來看看如何解決這個問題吧!2024-03-03