PYQT5(02)-QtDesigner使用

    新建窗体主要分为以下几种

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进行资源的转换

最后导入并调用

PyQt5是一个Python绑定Qt库的工具包,可以用于创建桌面应用程序。Qt Designer是一个用于创建Qt界面的可视化工具,可以方便地创建GUI界面并导出为.ui文件。 要使用Qt Designer编写PyQt5界面,可以按照以下步骤进行操作: 1. 安装PyQt5Qt Designer 如果你还没有安装PyQt5Qt Designer,可以使用以下命令进行安装: ``` pip install PyQt5 pyqt5-tools ``` 2. 创建Qt Designer界面 打开Qt Designer,创建一个新的界面。 3. 设计界面 在Qt Designer中,你可以从工具箱中拖拽控件到界面中,设置控件的属性,布局等。 4. 保存界面 在Qt Designer中,选择“文件”->“保存”,将界面保存为.ui文件。 5. 将.ui文件转换为.py文件 使用以下命令将.ui文件转换为.py文件: ``` pyuic5 -o ui_filename.py ui_filename.ui ``` 其中,ui_filename是你的.ui文件名。这将生成一个.py文件,其中包含Qt Designer界面的Python代码。 6. 编写程序 在Python代码中导入生成的.py文件,然后使用它来创建GUI界面。 下面是一个简单的示例程序: ```python from PyQt5 import QtWidgets, uic class MainWindow(QtWidgets.QMainWindow): def __init__(self): super(MainWindow, self).__init__() # Load the ui file uic.loadUi('ui_filename.ui', self) if __name__ == '__main__': app = QtWidgets.QApplication([]) window = MainWindow() window.show() app.exec_() ``` 在此示例中,我们使用`uic.loadUi`方法将.ui文件加载到`MainWindow`类中,然后创建`QApplication`和`MainWindow`实例,并将窗口显示出来。 以上就是使用Qt Designer编写PyQt5界面的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AdolphW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值