Python桌面应用开发(PyQT)入门思维导图,果断收藏

人工智能学习路径



PyQt5是干嘛的?

图形化界面设计,通俗的说就是软件开发,PyQt5就是Python用来开发软件的一个强大的第三方框架。类似于TkInter和WxPython,学习PyQt5教程后就能用python语言开发桌面应用软件程序。

为什么学习PyQt5?

先看的介绍,下面截取部分图片

清明放假期间,抽时间去做了Python桌面应用开发的学习路线,今天分享给大家。

桌面应用开发(PyQT)学习路线

核心案例代码

QTabWidget容器控件

from PyQt5.Qt import *
import sys
 
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QTabWidget容器控件")
        self.resize(600, 500)
        self.func_list()
 
    def func_list(self):
        self.func()
 
    def func(self):
        self.tab = QTabWidget(self)
        self.tab.resize(500, 400)
        # 创建三个窗口控件
        self.tab1 = QWidget()
        self.tab2 = QWidget()
        self.tab3 = QWidget()
        # 添加窗口到QTabWidget容器控件
        self.tab.addTab(self.tab1, 窗口一)
        self.tab.addTab(self.tab2, 窗口二)
        self.tab.addTab(self.tab3, 窗口三)
        self.tab_ui1()
 
    def tab_ui1(self):
        layout = QFormLayout()
        layout.addRow(账号:, QLineEdit())
        layout.addRow(密码:, QLineEdit())
        layout.addRow(QPushButton(点击登陆))
        self.tab1.setLayout(layout)  # 把布局设置到界面1上面
        self.tab.setTabText(0, 登陆界面)  # 更改界面名称
        self.tab.setTabText(1, 注册界面)
        self.tab.setTabText(2, 找回密码)
 
 
if __name__ == __main__:
    app = QApplication(sys.argv)
    window = Window()
 
    window.show()
    sys.exit(app.exec_())

QStackedWidget堆栈窗口控件

from PyQt5.Qt import *
import sys
 
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QStackedWidget堆栈窗口控件")
        self.resize(600, 500)
        self.func_list()
 
    def func_list(self):
        self.func()
 
    def func(self):
        # 创建列表
        self.lists = QListWidget()
        self.lists.insertItem(0, 登陆界面)
        self.lists.insertItem(1, 注册界面)
        self.lists.insertItem(2, 找回密码)
 
        # 创建三个窗口
        self.win1 = QWidget()
        self.win2 = QWidget()
        self.win3 = QWidget()
 
        # 创建堆栈窗口
        self.stack = QStackedWidget()
        self.stack.addWidget(self.win1)
        self.stack.addWidget(self.win2)
        self.stack.addWidget(self.win3)
 
        # 创建3个窗口内的控件
        layout = QFormLayout()
        layout.addRow(账号:, QLineEdit())
        layout.addRow(密码:, QLineEdit())
        layout.addRow(QPushButton(点击登陆))
        self.win1.setLayout(layout)
 
        layout1 = QFormLayout()
        layout1.addRow(账号:, QLineEdit())
        layout1.addRow(密码:, QLineEdit())
        layout1.addRow(验证码:, QLineEdit())
        layout1.addRow(QPushButton(点击注册))
        self.win2.setLayout(layout1)
 
        btn = QPushButton(按钮)
        layout2 = QFormLayout()
        layout2.addWidget(btn)
        self.win3.setLayout(layout2)
 
        # 布局界面
        box = QHBoxLayout()
        box.addWidget(self.lists)
        box.addWidget(self.stack)
        self.setLayout(box)
 
        # 增加点击切换界面
        self.lists.currentRowChanged.connect(self.display)
 
    def display(self, index):
        self.stack.setCurrentIndex(index)
 
 
if __name__ == __main__:
    app = QApplication(sys.argv)
    window = Window()
 
    window.show()
    sys.exit(app.exec_())

垂直布局QVBoxLayout

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
 
 
class Demo(QWidget):
 
    def __init__(self):
        super(Demo, self).__init__()
        self.user_label = QLabel(Username:, self)
        self.pwd_label = QLabel(Password:, self)
 
        self.v_layout = QVBoxLayout()                   # 1
        self.v_layout.addWidget(self.user_label)        # 2
        self.v_layout.addWidget(self.pwd_label)         # 3
 
        self.setLayout(self.v_layout)                   # 4
 
 
if __name__ == __main__:
    app = QApplication(sys.argv)
    demo = Demo()
    demo.show()
    sys.exit(app.exec_())

网格布局QGridLayout

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, 
    QGridLayout, QVBoxLayout, QHBoxLayout


class Demo(QWidget):

    def __init__(self):
        super(Demo, self).__init__()

        self.user_label = QLabel(Username:, self)
        self.pwd_label = QLabel(Password:, self)
        self.user_line = QLineEdit(self)
        self.pwd_line = QLineEdit(self)
        self.login_button = QPushButton(Log in, self)
        self.signin_button = QPushButton(Sign in, self)

        self.grid_layout = QGridLayout()                                # 1
        self.h_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()

        self.grid_layout.addWidget(self.user_label, 0, 0, 1, 1)         # 2
        self.grid_layout.addWidget(self.user_line, 0, 1, 1, 1)
        self.grid_layout.addWidget(self.pwd_label, 1, 0, 1, 1)
        self.grid_layout.addWidget(self.pwd_line, 1, 1, 1, 1)
        self.h_layout.addWidget(self.login_button)                     
        self.h_layout.addWidget(self.signin_button)
        self.v_layout.addLayout(self.grid_layout)                       # 3
        self.v_layout.addLayout(self.h_layout)                          # 4

        self.setLayout(self.v_layout)


if __name__ == __main__:
    app = QApplication(sys.argv)
    demo = Demo()
    demo.show()
    sys.exit(app.exec_())

综合设计-嵌套布局

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout, QPushButton


class MyWindow(QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle(嵌套布局示例)

        # 全局布局(1个):水平
        wlayout = QHBoxLayout()
        # 局部布局(4个):水平、竖直、网格、表单
        hlayout = QHBoxLayout()
        vlayout = QVBoxLayout()
        glayout = QGridLayout()
        formlayout = QFormLayout()

        # 局部布局添加部件(例如:按钮)
        hlayout.addWidget(QPushButton(str(1)))
        hlayout.addWidget(QPushButton(str(2)))
        vlayout.addWidget(QPushButton(str(3)))
        vlayout.addWidget(QPushButton(str(4)))
        glayout.addWidget(QPushButton(str(5)), 0, 0)
        glayout.addWidget(QPushButton(str(6)), 0, 1)
        glayout.addWidget(QPushButton(str(7)), 1, 0)
        glayout.addWidget(QPushButton(str(8)), 1, 1)
        formlayout.addWidget(QPushButton(str(9)))
        formlayout.addWidget(QPushButton(str(10)))
        formlayout.addWidget(QPushButton(str(11)))
        formlayout.addWidget(QPushButton(str(12)))

        # 准备四个部件
        hwg = QWidget()
        vwg = QWidget()
        gwg = QWidget()
        fwg = QWidget()

        # 四个部件设置局部布局
        hwg.setLayout(hlayout)
        vwg.setLayout(vlayout)
        gwg.setLayout(glayout)
        fwg.setLayout(formlayout)

        # 四个部件加至全局布局
        wlayout.addWidget(hwg)
        wlayout.addWidget(vwg)
        wlayout.addWidget(gwg)
        wlayout.addWidget(fwg)

        # 窗体本体设置全局布局
        self.setLayout(wlayout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())
经验分享 程序员 微信小程序 职场和发展