1、配置Qt designer外部工具
实质就是
Qt\bin
工具中designer.exe
请查看 PyQt5开始入门、pyQt5 + pycharm 已经说明
2、Qt designer(Qt 设计师)使用
2.1 创建保存文件ui
文件
apptest.ui
这个就是PyQt5
的布局文件;
文件apptest.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
2.2 pyuic5.exe 工具 转化成为py文件
可以使用
pyuic5.exe 工具
转化成为apptest.py文件
(查看 PyQt5开始入门、pyQt5 + pycharm):pyuic5 apptest.ui -o apptest.py
文件apptest.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'apptest.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
添加main入口使用:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
2.3 直接导入UI文件
uic.loadUi(r'C:\Users\Administrator\Desktop\apptest.ui', self)
导入UI文件from PyQt5.uic import loadUi
- UI文件中
<widget class="QMainWindow" name="MainWindow">
默认self
;对应代码class Ui_MainWindow(QMainWindow)
- 其他widget可直接通过
name
使用,例如self.statusbar.showMessage('Ready')
再举例添加菜单栏self.menubar.addAction(openAct)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog
from PyQt5.uic import loadUi
class Ui_MainWindow(QMainWindow):
def __init__(self):
super().__init__()
loadUi(r'C:\Users\Administrator\Desktop\apptest.ui', self)
self.setupUi()
def setupUi(self):
print("setupUI")
self.resize(800, 600)
self.statusbar.showMessage('Ready')
self.menuBarUI()
QtCore.QMetaObject.connectSlotsByName(self)
def menuBarUI(self):
openAct = QAction(self.menubar)
openAct.setCheckable(False)
openAct.setObjectName('openFileAction')
openAct.triggered.connect(self.opendir)
openAct.setText('打开')
self.menubar.addAction(openAct)
def opendir(self):
dir = QFileDialog.getExistingDirectory(self, r"Open Directory",
"./",
QFileDialog.ShowDirsOnly
| QFileDialog.DontResolveSymlinks)
print(str(dir))
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = Ui_MainWindow()
ui.show()
sys.exit(app.exec_())
2、qrc资源管理器
**qrc文件:**更多查看 PyQt5 资源管理文章来源:https://www.toymoban.com/news/detail-601529.html
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
<file>images/icon.ico</file>
</qresource>
</RCC>
文章来源地址https://www.toymoban.com/news/detail-601529.html
到了这里,关于使用 Qt designer的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!