欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5 GUI 開發(fā)的基礎知識

 更新時間:2025年07月27日 09:26:53   作者:吐吐搬磚  
Qt是一個跨平臺的C++圖形用戶界面開發(fā)框架,支持GUI和非GUI程序開發(fā),本文介紹了使用PyQt5進行界面開發(fā)的基礎知識,包括創(chuàng)建簡單窗口、常用控件、窗口屬性設置以及三種布局方式,感興趣的可以了解一下

簡介

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實現(xiàn)為PDF文檔設置和移除密碼

    使用Python實現(xiàn)為PDF文檔設置和移除密碼

    在數(shù)字化時代,文檔的安全性變得越來越重要,特別是對于包含敏感信息的PDF文件,所以本文主要來和大家介紹一下如何使用Python實現(xiàn)為PDF文檔設置和移除密碼,需要的可以參考下
    2024-03-03
  • Python中String模塊示例詳解

    Python中String模塊示例詳解

    string模塊主要包含關于字符串的處理函數(shù),這篇文章主要介紹了Python中String模塊示例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • 如何利用Python獲取文本中的電話號碼實例代碼

    如何利用Python獲取文本中的電話號碼實例代碼

    Python的文本處理是經(jīng)常碰到的一個問題,下面這篇文章主要給大家介紹了關于如何利用Python獲取文本中的電話號碼的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • python簡單貪吃蛇開發(fā)

    python簡單貪吃蛇開發(fā)

    這篇文章主要為大家詳細介紹了python簡單貪吃蛇開發(fā),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Python下載網(wǎng)絡文本數(shù)據(jù)到本地內存的四種實現(xiàn)方法示例

    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ù)采集和顯示

    文章介紹了如何使用Python和PyQt5實現(xiàn)串口數(shù)據(jù)采集和實時通信,首先,文章詳細描述了環(huán)境搭建步驟,包括Python和PyQt5的安裝和配置,文章詳細說明了程序實現(xiàn)的各個步驟,包括串口設置、數(shù)據(jù)讀取、數(shù)據(jù)發(fā)送和報文解析等,需要的朋友可以參考下
    2025-05-05
  • keras實現(xiàn)VGG16方式(預測一張圖片)

    keras實現(xiàn)VGG16方式(預測一張圖片)

    這篇文章主要介紹了keras實現(xiàn)VGG16方式(預測一張圖片),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • python 利用文件鎖單例執(zhí)行腳本的方法

    python 利用文件鎖單例執(zhí)行腳本的方法

    今天小編就為大家分享一篇python 利用文件鎖單例執(zhí)行腳本的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • 解決Python中的ModuleNotFoundError:?No?module?named?'paddle'錯誤

    解決Python中的ModuleNotFoundError:?No?module?named?'pad

    你是否在嘗試導入`paddle`模塊時遇到了"ModuleNotFoundError:?No?module?named?'paddle'"這個錯誤?別擔心,我們的指南會告訴你如何解決,這就像找到丟失的鑰匙一樣簡單,讓我們一起來看看如何解決這個問題吧!
    2024-03-03
  • 深入理解Python 關于supper 的 用法和原理

    深入理解Python 關于supper 的 用法和原理

    這篇文章主要介紹了Python 關于supper 的 用法和原理分析,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2018-02-02

最新評論