Qt Designer and Python: Build Your GUI
1.install pyside6
2.pyside6-designer.exe 发送到桌面快捷方式
3. 打开pyside6-designer 设计UI
4.保存为simple.ui 文件,再转成py文件
用代码执行
用代码执行 pyside6-uic.exe simple.ui -o simple.py
生成源文件simple.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | # -*- coding: utf-8 -*- ################################################################################ ## Form generated from reading UI file 'simple.ui' ## ## Created by: Qt User Interface Compiler version 6.8.0 ## ## 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 (QAbstractButton, QApplication, QDialog, QDialogButtonBox, QLabel, QSizePolicy, QTextEdit, QWidget) class Ui_Dialog( object ): def setupUi( self , Dialog): if not Dialog.objectName(): Dialog.setObjectName(u "Dialog" ) Dialog.resize( 736 , 520 ) self .buttonBox = QDialogButtonBox(Dialog) self .buttonBox.setObjectName(u "buttonBox" ) self .buttonBox.setGeometry(QRect( 50 , 360 , 341 , 32 )) self .buttonBox.setOrientation(Qt.Orientation.Horizontal) self .buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Cancel|QDialogButtonBox.StandardButton.Ok) self .txtName = QTextEdit(Dialog) self .txtName.setObjectName(u "txtName" ) self .txtName.setGeometry(QRect( 340 , 110 , 221 , 31 )) self .txtpassword = QTextEdit(Dialog) self .txtpassword.setObjectName(u "txtpassword" ) self .txtpassword.setGeometry(QRect( 340 , 170 , 221 , 31 )) self .label = QLabel(Dialog) self .label.setObjectName(u "label" ) self .label.setGeometry(QRect( 230 , 120 , 61 , 21 )) font = QFont() font.setPointSize( 14 ) self .label.setFont(font) self .label.setLineWidth( 3 ) self .label_2 = QLabel(Dialog) self .label_2.setObjectName(u "label_2" ) self .label_2.setGeometry(QRect( 220 , 180 , 71 , 21 )) self .label_2.setFont(font) self .retranslateUi(Dialog) self .buttonBox.accepted.connect(Dialog.objectName) self .buttonBox.rejected.connect(Dialog.objectName) QMetaObject.connectSlotsByName(Dialog) # setupUi def retranslateUi( self , Dialog): Dialog.setWindowTitle(QCoreApplication.translate( "Dialog" , u "Dialog" , None )) self .label.setText(QCoreApplication.translate( "Dialog" , u "\u59d3\u540d" , None )) self .label_2.setText(QCoreApplication.translate( "Dialog" , u "\u5bc6\u7801" , None )) # retranslateUi |
加上点击事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | # -*- coding: utf-8 -*- ################################################################################ ## Form generated from reading UI file 'simple.ui' ## ## Created by: Qt User Interface Compiler version 6.8.0 ## ## 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 (QAbstractButton, QApplication, QDialog, QDialogButtonBox, QLabel, QSizePolicy, QTextEdit, QWidget,QVBoxLayout) ''' class CustomDialog(QDialog): """ """ def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("HELLO!") QBtn = ( QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel ) self.buttonBox = QDialogButtonBox(QBtn) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) layout = QVBoxLayout() message = QLabel("Something happened, is that OK?") layout.addWidget(message) layout.addWidget(self.buttonBox) self.setLayout(layout) def button_clicked(self, s): print("click", s) dlg = CustomDialog() if dlg.exec(): print("Success!") else: print("Cancel!") ''' class Ui_Dialog( object ): """ """ def setupUi( self , Dialog): """ :param Dialog: :return: """ if not Dialog.objectName(): Dialog.setObjectName(u "Dialog" ) Dialog.resize( 736 , 520 ) self .buttonBox = QDialogButtonBox(Dialog) self .buttonBox.setObjectName(u "buttonBox" ) self .buttonBox.setGeometry(QRect( 90 , 280 , 341 , 32 )) self .buttonBox.setOrientation(Qt.Orientation.Horizontal) self .buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Cancel|QDialogButtonBox.StandardButton.Ok) self .txtName = QTextEdit(Dialog) self .txtName.setObjectName(u "txtName" ) self .txtName.setGeometry(QRect( 340 , 110 , 221 , 31 )) self .txtpassword = QTextEdit(Dialog) self .txtpassword.setObjectName(u "txtpassword" ) self .txtpassword.setGeometry(QRect( 340 , 170 , 221 , 31 )) self .label = QLabel(Dialog) self .label.setObjectName(u "label" ) self .label.setGeometry(QRect( 230 , 120 , 61 , 21 )) font = QFont() font.setPointSize( 14 ) self .label.setFont(font) self .label.setLineWidth( 3 ) self .label_2 = QLabel(Dialog) self .label_2.setObjectName(u "label_2" ) self .label_2.setGeometry(QRect( 230 , 180 , 71 , 21 )) self .label_2.setFont(font) self .retranslateUi(Dialog) self .buttonBox.accepted.connect(Dialog.objectName) self .buttonBox.rejected.connect(Dialog.objectName) self .buttonBox.clicked.connect( self .buttonClicked) ''' layout = QVBoxLayout() message = QLabel("Something happened, is that OK?") layout.addWidget(message) layout.addWidget(self.buttonBox) self.setLayout(layout) layout = QVBoxLayout() #message = QLabel("Something happened, is that OK?") #layout.addWidget(message) layout.addWidget(self.buttonBox) Dialog.setLayout(layout) ''' QMetaObject.connectSlotsByName(Dialog) # setupUi def buttonClicked( self ,button): role = self .buttonBox.standardButton(button) if role = = QDialogButtonBox.StandardButton.Ok: print ( "Save clicked" ) elif role = = QDialogButtonBox.StandardButton.Cancel: print ( "Cancel clicked" ) def retranslateUi( self , Dialog): Dialog.setWindowTitle(QCoreApplication.translate( "Dialog" , u "Dialog\u6d4b\u8bd5" , None )) #if QT_CONFIG(tooltip) self .buttonBox.setToolTip(QCoreApplication.translate( "Dialog" , u "\u6309\u786e\u5b9a" , None )) #endif // QT_CONFIG(tooltip) self .label.setText(QCoreApplication.translate( "Dialog" , u "\u59d3\u540d" , None )) self .label_2.setText(QCoreApplication.translate( "Dialog" , u "\u5bc6\u7801" , None )) # retranslateUi |
python.exe -m pip install --upgrade pip
pip install pyside6
pip install pyside6 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyqt6
引用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | # -*- coding: utf-8 -*- # 版权所有 2024 涂聚文有限公司 # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:pip install pyqt6 python.exe -m pip install --upgrade pip pip install pyside6 # pip install pyside6 -i https://pypi.tuna.tsinghua.edu.cn/simple # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, poostgreSQL 17.0 # Datetime : 2024/12/7 23:37 # User : geovindu # Product : PyCharm # Project : Pysimple # File : simply2.py # explain : 学习 import sys from PySide6.QtWidgets import QApplication,QWidget from simple import Ui_Dialog class Windows(Ui_Dialog,QWidget): """ 多重继承 继承UI 设计的文件 """ def __init__( self ): """ """ super ().__init__() self .setupUi( self ) if __name__ = = '__main__' : app = QApplication(sys.argv) win = Windows() win.show() m = app. exec () sys.exit(m) |
运行:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2023-01-25 CSharp: emojione
2022-01-25 java: itext 7.2.1 using jdk 14.0.2
2022-01-25 java: itext 7.2.1 using jdk 17.0.1