PyQT5之使用QT Designer创建基本窗口方式
作者:Trivis?Kylee 时间:2023-10-06 05:59:41
环境配置
pip install pyqt5 (安装pyQT5库):pyuic5包含在内
pip install pyqt5-tools (安装QT-Designer)
使用QT Designer布局窗体
UI文件的编译
将.UI文件编译为.PY文件(CMD命令行中):
pyuic5 xxx.ui -o xxx.py
#不可以存在中文!
打开编译后的.py文件,新建SLOT()函数并修改连接函数:
在末尾添加运行代码:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
编译后的.PY源码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Users\Geclipse\Desktop\untitled.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
MainWindow.setMouseTracking(False)
MainWindow.setWindowOpacity(1.0)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(300, 0, 161, 71))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.label.setFont(font)
self.label.setObjectName("label")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(20, 220, 751, 321))
self.textEdit.setObjectName("textEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(300, 90, 141, 61))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
self.menu_1 = QtWidgets.QMenu(self.menubar)
self.menu_1.setObjectName("menu_1")
self.menu_2 = QtWidgets.QMenu(self.menubar)
self.menu_2.setObjectName("menu_2")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionFunction1 = QtWidgets.QAction(MainWindow)
self.actionFunction1.setObjectName("actionFunction1")
self.actionFunction2 = QtWidgets.QAction(MainWindow)
self.actionFunction2.setObjectName("actionFunction2")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.actionExit_2 = QtWidgets.QAction(MainWindow)
self.actionExit_2.setObjectName("actionExit_2")
self.menu_1.addAction(self.actionFunction1)
self.menu_1.addAction(self.actionFunction2)
self.menu_1.addSeparator()
self.menu_1.addAction(self.actionExit_2)
self.menubar.addAction(self.menu_1.menuAction())
self.menubar.addAction(self.menu_2.menuAction())
self.textEdit.setText("pyuic untitled.ui -o untitled.py")
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.add_hello_qt)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def add_hello_qt(self):
self.textEdit.setText("Hello QT!")
#★★★ def retranslateUi(eslf,MainWindow)
#translate翻译
#retranslate重译(对界面中控件所进行的其他调整:如设置标题会出现在这里)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "This is a simple title"))
self.label.setText(_translate("MainWindow", "Simple Widget"))
self.pushButton.setStatusTip(_translate("MainWindow", "click this will add ‘hello QT' in below area"))
self.pushButton.setText(_translate("MainWindow", "Hello QT"))
self.menu_1.setTitle(_translate("MainWindow", "菜单栏1"))
self.menu_2.setTitle(_translate("MainWindow", "菜单栏2"))
self.actionFunction1.setText(_translate("MainWindow", "Function1"))
self.actionFunction2.setText(_translate("MainWindow", "Function2"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
self.actionExit_2.setText(_translate("MainWindow", "Exit"))
#下面的这些部分需要手动打开py文件进行添加
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
运行结果
来源:https://kylee.blog.csdn.net/article/details/90549897
标签:PyQT5,QT,Designer,创建窗口
0
投稿
猜你喜欢
Python unittest模块用法实例分析
2023-03-28 17:36:06
Python通过调用mysql存储过程实现更新数据功能示例
2024-01-25 11:28:51
可以拖动的div 实现代码第1/2页
2024-03-19 17:46:20
Pytorch创建张量的四种方法
2023-11-20 15:25:36
用Python解决x的n次方问题
2022-02-02 06:30:10
大幅提升MySQL中InnoDB的全表扫描速度的方法
2024-01-19 20:58:42
基于python实现学生管理系统
2021-11-24 21:07:48
po+selenium+unittest自动化测试项目实战
2022-05-03 14:46:46
SQL Server中处理空值时涉及的三问题
2009-01-20 11:24:00
Python实现App自动签到领取积分功能
2021-09-16 15:59:59
如何更改Pycharm配置文件的存放路径
2023-12-29 03:11:06
设计和布局之间的思考
2008-10-09 13:06:00
python中pivot()函数基础知识点
2023-12-18 15:28:06
Spring Boot整合 NoSQL 数据库 Redis详解
2024-01-29 18:16:36
Go语言使用钉钉机器人推送消息的实现示例
2024-05-09 14:57:37
语言编程花絮内建构建顺序示例详解
2023-11-04 09:42:12
Python字符串处理实现单词反转
2021-01-30 19:19:01
JS中的THIS和WINDOW.EVENT.SRCELEMENT详解
2023-07-20 20:48:51
python TCP Socket的粘包和分包的处理详解
2021-06-14 16:49:50
跟老齐学Python之集合的关系
2023-02-04 22:32:27