Python使用QtSide6(PyQt)编写界面

1、安装QtSide6

开始菜单cmd

创建虚拟环境

复制代码
python -m venv env2

进入虚拟环境

复制代码
call env2/scripts/activate

安装Pyside6

复制代码
pip install Pyside6

2、设计Qt界面

打开designer.exe,设计界面

点击菜单【窗体】【View Python Code...】,点击【全部复制】按钮进行复制

3、使用Qt界面

打开PyCharm,新建工程,选择虚拟环境的python.exe

新建ui.py,将designer中的Python代码粘贴过去

python 复制代码
# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'untitledugPqGJ.ui'
##
## Created by: Qt User Interface Compiler version 6.7.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QDialog, QLineEdit, QPushButton,
    QSizePolicy, QWidget)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if not Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(400, 300)
        self.pushButton = QPushButton(Dialog)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(200, 220, 75, 24))
        self.lineEdit = QLineEdit(Dialog)
        self.lineEdit.setObjectName(u"lineEdit")
        self.lineEdit.setGeometry(QRect(130, 70, 113, 20))

        self.retranslateUi(Dialog)
        QMetaObject.connectSlotsByName(Dialog)
    # setupUi

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
        self.pushButton.setText(QCoreApplication.translate("Dialog", u"\u70b9\u51fb", None))
    # retranslateUi

新建main.py

python 复制代码
from symtable import Class

from ui import Ui_Dialog
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton
from PySide6 import QtCore, QtGui

class Main(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

    def on_clicked2(self):
        auto_ui_window.lineEdit.setText("clicked2")
        print("Button click2")

def on_clicked1():
    auto_ui_window.lineEdit.setText("clicked1")
    print("button click")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = Main()
    auto_ui_window = Ui_Dialog()
    auto_ui_window.setupUi(main_window)

    #事件处理方法1
    #auto_ui_window.pushButton.clicked.connect(on_clicked1)

    # 事件处理方法2
    QtCore.QObject.connect(auto_ui_window.pushButton, QtCore.SIGNAL("clicked()"), main_window,QtCore.SLOT('on_clicked2()'))

    main_window.show()
    app.exec()
    sys.exit(app.exec())

运行效果

相关推荐
花酒锄作田4 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪8 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽8 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战9 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋15 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama