目录
模板 | 说明 |
Dialog | 基于 QDialog 类的窗体,具有一般对话框的特性,如可以模态显示、具有返回值等。 |
Main Window | 基于 QMainWindow 类的窗体,具有主窗口的特性,窗口上有主菜单栏、工具栏、状态栏等。 |
Widget | 基于 QWidget 类的窗体。QWidget 类是所有界面组件的基类,如 QLabel、QPushBtton 等界面组件都是从 QWidget 类继承而来的。QWidget 类也是 QDialog、QMainWindow 的父类,基于 QWidget 类创建的窗体可以作为独立的窗口运行,也可以嵌入到其他界面组件内显示。 |
1 Dialog
1.1 打开 designer.exe ,文件--新建--Dialog without Buttons--创建。
1.2 保存为 Dialog.ui。
1.3 使用 pyuic5 转换为 Dialog.py。
C:\Python\Python38-32\Scripts\pyuic5 -x Dialog.ui -o Dialog.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Dialog.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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
1.4 运行 Dialog.py
C:\Python\Python38-32\python.exe Dialog.py
2 Main Window
2.1 打开 designer.exe ,文件--新建--Main Window--创建。
2.2 保存为 MainWindow.ui 。
2.3 使用 pyuic5 转换MainWindow.py。
C:\Python\Python38-32\Scripts\pyuic5.exe -x MainWindow.ui -o MainWindow.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.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"))
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_())
2.4 运行 MainWindow.py。
C:\Python\Python38-32\python.exe MainWindow.py
3 Widget
3.1 打开 designer.exe ,文件--新建--Widget--创建。
3.2 保存为 Widget.ui。
3.3 使用 pyuic5 转换 Widget.py。
C:\Python\Python38-32\Scripts\pyuic5.exe -x Widget.ui -o Widget.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Widget.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_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
3.4 运行 Widget.py。
C:\Python\Python38-32\python.exe Widget.py
其他:
创建一个调用 callWidget.py
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from Widget import Ui_Form
class QmyWidget(QWidget):
def __init__(self, parent=None):
# 调用父类构造函数,创建 QWidget 窗体
super().__init__(parent)
# 创建 UI 对象
self.ui = Ui_Form()
# 构造 UI
self.ui.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
Form = QmyWidget()
Form.show()
sys.exit(app.exec_())
'''
app = QApplication(sys.argv)
Form = QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
'''
参考:
《Python Qt GUI与数据可视化编程》