Skip to content

pyQt编译以及安装

Qt是一个C++的库, 这一个适用于开发GUI程序的, 由于开源社区的存在, 这一个可以使用python进行开发

这一个是一个跨平台的程序, 支持Windows, Linux, Unix, IOS, Android, 嵌入式等

pyQt是C++写的, 使用python进行调用的程序, 使用GPL协议

还有一个PySide2也是一个使用python进行图形化开发的库, 但是使用的数量不如pyQt

bash
pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install PyQt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple

安装PyQt5-tools之后可以使用image-20240506225939527

这一个进行图形化开发

python
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
print(QT_VERSION_STR)

测试

python
import sys
from PyQt5.QtWidgets import QWidget, QApplication


# 创建一个app对象, 参数是这一个程序运行的变量
app = QApplication(sys.argv)
widget = QWidget()
widget.resize(640, 480)
widget.setWindowTitle("Hello, PyQt5!")
widget.show()
# 执行这一个app
sys.exit(app.exec())

基础模块

  • QtCore: 核心的非GUI的功能, 主要是时间, 文件文件夹, 各种数据, 流, URLs, mine类文件, 进程和线程使用
  • QtGui: 窗口系统, 事件处理, 2D图像, 基本绘图, 字体文字类
  • QtWidget: 插件, 应用UI元素
python
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Window1(QWidget):
    def __init__(self):
        super().__init__()
        QLabel("抽屉1", self)
        self.setStyleSheet("background-color:red;")
        
    
class Window2(QWidget):
    def __init__(self):
        super().__init__()
        QLabel("抽屉2", self)
        self.setStyleSheet("background-color:green;")

class MyWindow(QWidget):
    def __init__(self):

        super().__init__()
        self.create_stacked_layout()
        self.init_ui()
    def create_stacked_layout(self):
        self.stacked_layout = QStackedLayout()
        win1 = Window1()
        win2 = Window2()
        self.stacked_layout.addWidget(win1)
        self.stacked_layout.addWidget(win2)

    def init_ui(self):
        self.setFixedSize(300, 270)

        container = QVBoxLayout()

        widget = QWidget()
        # 添加一个抽屉布局器
        widget.setLayout(self.stacked_layout)
        widget.setStyleSheet("background-color:grey;") # 这一个样式是CSS的样式
        # 两个按钮
        btn_press1 = QPushButton("1")
        btn_press2 = QPushButton("2")
	   # 设置按键的功能
        btn_press1.clicked.connect(self.btn_press1_clicked)
        btn_press2.clicked.connect(self.btn_press2_clicked)
        # 显示空间添加组件
        container.addWidget(widget)
        container.addWidget(btn_press1)
        container.addWidget(btn_press2)

        self.setLayout(container)



    def btn_press1_clicked(self):
        self.stacked_layout.setCurrentIndex(0)

    def btn_press2_clicked(self):
        self.stacked_layout.setCurrentIndex(1)
            


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    app.exec()

Vscode设置

在Visual Studio Code中使用PyQt5开发python GUI应用程序 - 知乎 (zhihu.com)

ui文件转py

image-20240508210545788

python
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'e:\JHY\python\2024-5-6-pyQt\2024-5-8-test2.ui'
#
# Created by: PyQt5 UI code generator 5.15.10
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(596, 198)
        self.formLayoutWidget = QtWidgets.QWidget(Form)
        self.formLayoutWidget.setGeometry(QtCore.QRect(40, 30, 251, 61))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.formLayout.setContentsMargins(0, 0, 0, 0)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(self.formLayoutWidget)
        self.label.setObjectName("label")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
        self.label_2 = QtWidgets.QLabel(self.formLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
        self.lineEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.lineEdit.setObjectName("lineEdit")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)
        self.textBrowser = QtWidgets.QTextBrowser(Form)
        self.textBrowser.setGeometry(QtCore.QRect(320, 20, 256, 151))
        self.textBrowser.setObjectName("textBrowser")
        self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(90, 110, 201, 41))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout.addWidget(self.pushButton_2)
        self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)

        self.retranslateUi(Form)
        self.pushButton_2.clicked.connect(self.textBrowser.hide) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "用户名"))
        self.label_2.setText(_translate("Form", "密码"))
        self.pushButton_2.setText(_translate("Form", "登录"))
        self.pushButton.setText(_translate("Form", "忘记密码"))

if __name__ == '__main__':
    app = QApplication(sys.argv)                        
    MainWindow = QWidget()                    
    ui = Ui_Form()     # ui=GUI的py文件名.类名
    ui.setupUi(MainWindow)	# 设置一个显示的窗口

    MainWindow.show()
    sys.exit(app.exec_())
python
from PyQt5 import uic
import UItest2 as UI

class MyWindow(UI.Ui_Form, QMainWindow):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.retranslateUi(self)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = MyWindow()
    w.show()

    app.exec()