新建窗体主要分为以下几种
Main Window | 主窗口(包括菜单栏工具栏等) |
Dialog without Buttons | |
Dialog with Buttons Right | |
Dialog with Buttons Bottom | |
Widget | 通用窗口(不知道使用什么窗口时就使用) |
属性编辑器用到的主要内容
objectName | 控件对象名称 |
geometry | 相对坐标系 |
sizePolicy | 控件策略 |
minimumSize/maximumSize | 最大最小长度 |
font | 字体 |
windowTitle | 窗口标题 |
windowIcon/icon | 窗口图标/控件图标 |
iconSize | 图标大小 |
toolTip | 提示信息 |
statusTip | 任务栏提示信息 |
txt | 控件文本 |
将.ui文件转换为.py文件
PYQT5(01)-在PyCharm中导入QtDesigner、pyuic5、pyrcc5_AdolphW的博客-CSDN博客
UI文件内容为XML格式,PyCharm可通过右键External Tools -> PyUIC转换为py格式
界面与逻辑分离
将MainWindow.ui转换为MainWindow.py并通过下面的方法继承界面文件的主窗口类即可
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from MainWin.MainWindow import Ui_MainWindow
class MainForm(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainForm, self).__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainForm()
win.show()
sys.exit(app.exec_())
布局管理
Vertical Layout | 垂直布局 | |
Horizontal Layout | 水平布局 | |
Grid Layout | 栅格布局 | |
Form Layout | 表单布局 |
应用
实现这一效果 Ctrl+R 预览
最左边一列在上方放一个空标签并对四个标签使用垂直布局
中间两列使用栅格布局
在布局间加上Spacers以及Line
最后对整体使用水平布局
导出代码如下:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(361, 122)
Form.setAutoFillBackground(False)
self.layoutWidget = QtWidgets.QWidget(Form)
self.layoutWidget.setGeometry(QtCore.QRect(10, 10, 336, 91))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label_6 = QtWidgets.QLabel(self.layoutWidget)
self.label_6.setText("")
self.label_6.setObjectName("label_6")
self.verticalLayout.addWidget(self.label_6)
self.label_3 = QtWidgets.QLabel(self.layoutWidget)
self.label_3.setObjectName("label_3")
self.verticalLayout.addWidget(self.label_3)
self.label_4 = QtWidgets.QLabel(self.layoutWidget)
self.label_4.setObjectName("label_4")
self.verticalLayout.addWidget(self.label_4)
self.label_5 = QtWidgets.QLabel(self.layoutWidget)
self.label_5.setObjectName("label_5")
self.verticalLayout.addWidget(self.label_5)
self.horizontalLayout.addLayout(self.verticalLayout)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.horizontalLayout.addItem(spacerItem)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(self.layoutWidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.layoutWidget)
self.doubleSpinBox.setObjectName("doubleSpinBox")
self.gridLayout.addWidget(self.doubleSpinBox, 1, 0, 1, 1)
self.doubleSpinBox_4 = QtWidgets.QDoubleSpinBox(self.layoutWidget)
self.doubleSpinBox_4.setObjectName("doubleSpinBox_4")
self.gridLayout.addWidget(self.doubleSpinBox_4, 1, 1, 1, 1)
self.doubleSpinBox_2 = QtWidgets.QDoubleSpinBox(self.layoutWidget)
self.doubleSpinBox_2.setObjectName("doubleSpinBox_2")
self.gridLayout.addWidget(self.doubleSpinBox_2, 2, 0, 1, 1)
self.doubleSpinBox_5 = QtWidgets.QDoubleSpinBox(self.layoutWidget)
self.doubleSpinBox_5.setObjectName("doubleSpinBox_5")
self.gridLayout.addWidget(self.doubleSpinBox_5, 2, 1, 1, 1)
self.doubleSpinBox_3 = QtWidgets.QDoubleSpinBox(self.layoutWidget)
self.doubleSpinBox_3.setObjectName("doubleSpinBox_3")
self.gridLayout.addWidget(self.doubleSpinBox_3, 3, 0, 1, 1)
self.doubleSpinBox_6 = QtWidgets.QDoubleSpinBox(self.layoutWidget)
self.doubleSpinBox_6.setObjectName("doubleSpinBox_6")
self.gridLayout.addWidget(self.doubleSpinBox_6, 3, 1, 1, 1)
self.horizontalLayout.addLayout(self.gridLayout)
self.line = QtWidgets.QFrame(self.layoutWidget)
self.line.setLineWidth(4)
self.line.setFrameShape(QtWidgets.QFrame.VLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.horizontalLayout.addWidget(self.line)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.label_5.setBuddy(self.doubleSpinBox_3)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label_3.setText(_translate("Form", "收益"))
self.label_4.setText(_translate("Form", "最大回撤"))
self.label_5.setText(_translate("Form", "&sharp比"))
self.label.setText(_translate("Form", "最大值"))
self.label_2.setText(_translate("Form", "最小值"))
self.pushButton.setText(_translate("Form", "OK"))
minimumSize/maximumSize属性在其中控制了button的大小
编辑控件 | ||
编辑信号和槽 | ||
编辑伙伴 | ||
编辑tab顺序 |
调用
# -*-encoding:utf-8 -*-
"""
@version:3.8
@time:2021/10/25 17:01
@author:AdolphWong
@title:
@file:layout_Manage.py
"""
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication
from layouyManage import Ui_Form
class LayoutDemo(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(LayoutDemo, self).__init__(parent)
self.setupUi(self)
# 一种信号与槽的绑定方法
@pyqtSlot()
def on_pushButton_clicked(self):
print("收益max:", self.doubleSpinBox_return_max.text())
print("收益min:", self.doubleSpinBox_return_min.text())
print("最大回撤max:", self.doubleSpinBox_maxdrawdown_max.text())
print("最大回撤min:", self.doubleSpinBox_maxdrawdown_min.text())
print("sharp比max:", self.doubleSpinBox_sharp_max.text())
print("sharp比max:", self.doubleSpinBox_sharp_min.text())
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = LayoutDemo()
ui.show()
sys.exit(app.exec_())
菜单栏与工具栏
MainWindow即主窗口,包含菜单栏、工具栏、任务栏等。
双击顶部添加菜单栏 --> 可通过“文件(&F)”方式添加快捷键
右键添加工具栏,Ctrl+R 预览
可通过右下角动作编辑器进行快捷键添加
双击需要编辑的动作可进行设置,添加图标快捷键等
设计如下主窗口:
文件中下拉菜单包含打开,新建以及关闭,工具栏包含添加窗体
转换代码如下
# -*- coding: utf-8 -*-
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")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 721, 531))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.MaingridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.MaingridLayout.setContentsMargins(0, 0, 0, 0)
self.MaingridLayout.setObjectName("MaingridLayout")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
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.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.fileOpenAction = QtWidgets.QAction(MainWindow)
self.fileOpenAction.setObjectName("fileOpenAction")
self.fileCloseAction = QtWidgets.QAction(MainWindow)
self.fileCloseAction.setObjectName("fileCloseAction")
self.fileNewAction = QtWidgets.QAction(MainWindow)
self.fileNewAction.setObjectName("fileNewAction")
self.addWinAction = QtWidgets.QAction(MainWindow)
self.addWinAction.setObjectName("addWinAction")
self.menu.addAction(self.fileOpenAction)
self.menu.addAction(self.fileNewAction)
self.menu.addAction(self.fileCloseAction)
self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menu_2.menuAction())
self.toolBar.addAction(self.addWinAction)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "内部打开例"))
self.menu.setTitle(_translate("MainWindow", "文件(&F)"))
self.menu_2.setTitle(_translate("MainWindow", "编辑(&E)"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.fileOpenAction.setText(_translate("MainWindow", "打开"))
self.fileOpenAction.setShortcut(_translate("MainWindow", "Alt+O"))
self.fileCloseAction.setText(_translate("MainWindow", "关闭"))
self.fileCloseAction.setShortcut(_translate("MainWindow", "Alt+C"))
self.fileNewAction.setText(_translate("MainWindow", "新建"))
self.fileNewAction.setShortcut(_translate("MainWindow", "Alt+N"))
self.addWinAction.setText(_translate("MainWindow", "添加窗体"))
代码位置分布如下
其中通过添加窗体添加一个窗体到主界面,并加载一张图片,添加的窗体如下
窗体名称为ChildrenForm,转换的py代码为
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ChildrenFrom.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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_ChildrenForm(object):
def setupUi(self, ChildrenForm):
ChildrenForm.setObjectName("ChildrenForm")
ChildrenForm.resize(300, 305)
self.label = QtWidgets.QLabel(ChildrenForm)
self.label.setGeometry(QtCore.QRect(10, 20, 231, 221))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap(":/ico/icon/Open.png"))
self.label.setObjectName("label")
self.retranslateUi(ChildrenForm)
QtCore.QMetaObject.connectSlotsByName(ChildrenForm)
def retranslateUi(self, ChildrenForm):
_translate = QtCore.QCoreApplication.translate
ChildrenForm.setWindowTitle(_translate("ChildrenForm", "Form"))
import pic.apprcc_rc
最后写逻辑文件,并命名为StartUp,如下
# -*-encoding:utf-8 -*-
"""
@version:3.8
@time:2021/10/16 10:45
@author:AdolphWong
@title:
@file:StartUp.py
"""
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QFileDialog
from MainWin.MainWindow import Ui_MainWindow
from MainWin.ChildrenFrom import Ui_ChildrenForm
class MainForm(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainForm, self).__init__()
self.setupUi(self)
# 生成子窗口
self.child = ChildrenForm()
# 关闭事件
self.fileCloseAction.triggered.connect(self.close)
# 打开事件
self.fileOpenAction.triggered.connect(self.openMsg)
# 单击加载子窗口
self.addWinAction.triggered.connect(self.childShow)
def openMsg(self):
file, ok = QFileDialog.getOpenFileNames(self, "打开", "C:/", "All Files(*);;Text Files (*.txt)")
# 状态栏显示文件地址
self.statusbar.showMessage(file)
def childShow(self):
# 添加子窗口
self.MaingridLayout.addWidget(self.child)
self.child.show()
class ChildrenForm(QWidget, Ui_ChildrenForm):
def __init__(self):
super(ChildrenForm, self).__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainForm()
win.show()
sys.exit(app.exec_())
打包资源文件
使用QtDesigner进行资源文件的创建
使用PYRCC进行资源的转换
最后导入并调用