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

运行效果

相关推荐
加德霍克1 小时前
【机器学习】使用scikit-learn中的KNN包实现对鸢尾花数据集或者自定义数据集的的预测
人工智能·python·学习·机器学习·作业
matlabgoodboy1 小时前
代码编写java代做matlab程序代编Python接单c++代写web系统设计
java·python·matlab
l1x1n01 小时前
No.37 笔记 | Python面向对象编程学习笔记:探索代码世界的奇妙之旅
笔记·python·学习
wanfeng_091 小时前
视频m3u8形式播放 -- python and html
python·html·video·hls·m3u8
阿俊仔(摸鱼版)2 小时前
Python 常用运维模块之OS模块篇
运维·开发语言·python·云服务器
MasterNeverDown2 小时前
WPF 使用iconfont
hadoop·ui·wpf
lly_csdn1232 小时前
【Image Captioning】DynRefer
python·深度学习·ai·图像分类·多模态·字幕生成·属性识别
西猫雷婶3 小时前
python学opencv|读取图像(四十一 )使用cv2.add()函数实现各个像素点BGR叠加
开发语言·python·opencv
金融OG3 小时前
99.11 金融难点通俗解释:净资产收益率(ROE)VS投资资本回报率(ROIC)VS总资产收益率(ROA)
大数据·python·算法·机器学习·金融
小唐C++4 小时前
C++小病毒-1.0勒索
开发语言·c++·vscode·python·算法·c#·编辑器