PyQt5GUI开发的基础知识

作者:

文章目录
  • QT的全称是"Qt Toolkit",是一个跨平台的C++图形用户界面应用程序开发框架。Qt(发音为"cute")是一个由Qt Company开发的跨平台应用程序开发框架,最初由挪威公司Trolltech于1991年创建。它主要用于开发图形用户界面(GUI)应用程序,但也支持非GUI程序的开发,如控制台工具和服务器。‌‌
  • 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_() # 程序进行循环等待状态
  • QtCore、QtGui、QtWidgets
  • 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_() # 程序进行循环等待状态
  • 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_() # 程序进行循环等待状态
  • 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_() # 程序进行循环等待状态
  • 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)
  • 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_()
  • 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 开发基础的文章就介绍到这了,更多相关PyQt5 GUI 开发内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客! 您可能感兴趣的文章: python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例 python GUI库图形界面开发之PyQt5开发环境配置与基础使用 python GUI库图形界面开发之PyQt5信号与槽机制、自定义信号基础介绍
  • 目录
    • 简介
    • 第一个PyQt程序
    • 最常用的三个功能模块
    • 控件QPushButton(按钮)
    • 控件QLable(纯文本)
    • 控件QLineEdit(文本框)
    • 调整窗口的大小和位置
    • 设置窗口的图标
    • 布局-QBoxLayout
      • 垂直布局-QVBoxLayout
      • 水平布局-QHBoxLayout
    • 布局-QGridLayout
      • 布局-QFormLayout

        QT的全称是"Qt Toolkit",是一个跨平台的C++图形用户界面应用程序开发框架。
        Qt(发音为"cute")是一个由Qt Company开发的跨平台应用程序开发框架,最初由挪威公司Trolltech于1991年创建。它主要用于开发图形用户界面(GUI)应用程序,但也支持非GUI程序的开发,如控制台工具和服务器。‌‌

        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_()                        # 程序进行循环等待状态
        

        QtCore、QtGui、QtWidgets

        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_()                        # 程序进行循环等待状态
        

        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_()                        # 程序进行循环等待状态
        

        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_()                        # 程序进行循环等待状态
        

        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)
        

        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_()
        

        """"""
        
        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_()
        

        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_()
        

        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 开发基础的文章就介绍到这了,更多相关PyQt5 GUI 开发内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!

        您可能感兴趣的文章:

        • python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例
        • python GUI库图形界面开发之PyQt5开发环境配置与基础使用
        • python GUI库图形界面开发之PyQt5信号与槽机制、自定义信号基础介绍

        站内搜索