首页 > 编程语言 >python pyqt5学习记录(三)

python pyqt5学习记录(三)

时间:2024-07-07 09:26:25浏览次数:19  
标签:sizePolicy 记录 python LayoutDemo self doubleSpinBox pyqt5 QtWidgets label

一、布局

在PyQt5中,可以使用QHBoxLayout来创建水平布局,使用QVBoxLayout来创建垂直布局。以下是一个简单的例子,展示了如何将两个按钮分别放置在水平和垂直布局中。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout


class ExampleApp(QWidget):
    def __init__(self):
        super().__init__()
        # super().__init__()是Python中用于在子类中调用父类初始化方法的一种机制,
        #它允许子类继承父类的初始化逻辑,同时允许添加或覆盖额外的初始化行为。
        self.initUI()

    def initUI(self):
        # 创建水平布局
        hbox = QHBoxLayout()
        button1 = QPushButton('Button1')
        button2 = QPushButton('Button2')
        hbox.addStretch(1)
        hbox.addWidget(button1)
        hbox.addStretch(1)
        hbox.addWidget(button2)
        hbox.addStretch(1)

        # 创建垂直布局
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        vbox.addStretch(1)

        # 设置窗口的布局
        self.setLayout(vbox)

        # 设置窗口的标题
        self.setWindowTitle('Horizontal and Vertical Layout')

        # 设置窗口的大小
        self.setGeometry(300, 300, 300, 200)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = ExampleApp()
    sys.exit(app.exec_())

在这个例子中,ExampleApp类继承自QWidget。在initUI方法中,我们创建了两个按钮,并将它们放置在QHBoxLayout中以创建水平布局。然后,我们将这个水平布局添加到了一个QVBoxLayout中,从而创建了一个垂直布局,并将这个垂直布局设置为窗口的主布局。

二、用Qt designer画完控件后的界面如下:

三、转化成*.py代码如下:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_LayoutDemo(object):
    def setupUi(self, LayoutDemo):
        LayoutDemo.setObjectName("LayoutDemo")
        LayoutDemo.resize(565, 430)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(3)
        sizePolicy.setVerticalStretch(6)
        sizePolicy.setHeightForWidth(LayoutDemo.sizePolicy().hasHeightForWidth())
        LayoutDemo.setSizePolicy(sizePolicy)
        self.centralwidget = QtWidgets.QWidget(LayoutDemo)
        self.centralwidget.setObjectName("centralwidget")
        self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QtCore.QRect(9, 9, 504, 94))
        self.layoutWidget.setObjectName("layoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
        self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
        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)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
        self.label_3.setSizePolicy(sizePolicy)
        self.label_3.setObjectName("label_3")
        self.verticalLayout.addWidget(self.label_3)
        self.label_4 = QtWidgets.QLabel(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_4.sizePolicy().hasHeightForWidth())
        self.label_4.setSizePolicy(sizePolicy)
        self.label_4.setObjectName("label_4")
        self.verticalLayout.addWidget(self.label_4)
        self.label_5 = QtWidgets.QLabel(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_5.sizePolicy().hasHeightForWidth())
        self.label_5.setSizePolicy(sizePolicy)
        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)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
        self.label.setSizePolicy(sizePolicy)
        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_returns_min = QtWidgets.QDoubleSpinBox(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.doubleSpinBox_returns_min.sizePolicy().hasHeightForWidth())
        self.doubleSpinBox_returns_min.setSizePolicy(sizePolicy)
        self.doubleSpinBox_returns_min.setMaximum(0.8)
        self.doubleSpinBox_returns_min.setSingleStep(0.01)
        self.doubleSpinBox_returns_min.setObjectName("doubleSpinBox_returns_min")
        self.gridLayout.addWidget(self.doubleSpinBox_returns_min, 1, 0, 1, 1)
        self.doubleSpinBox_returns_max = QtWidgets.QDoubleSpinBox(self.layoutWidget)
        self.doubleSpinBox_returns_max.setObjectName("doubleSpinBox_returns_max")
        self.gridLayout.addWidget(self.doubleSpinBox_returns_max, 1, 1, 1, 1)
        self.doubleSpinBox_maxdrawdown_min = QtWidgets.QDoubleSpinBox(self.layoutWidget)
        self.doubleSpinBox_maxdrawdown_min.setObjectName("doubleSpinBox_maxdrawdown_min")
        self.gridLayout.addWidget(self.doubleSpinBox_maxdrawdown_min, 2, 0, 1, 1)
        self.doubleSpinBox_maxdrawdown_max = QtWidgets.QDoubleSpinBox(self.layoutWidget)
        self.doubleSpinBox_maxdrawdown_max.setObjectName("doubleSpinBox_maxdrawdown_max")
        self.gridLayout.addWidget(self.doubleSpinBox_maxdrawdown_max, 2, 1, 1, 1)
        self.doubleSpinBox_sharp_min = QtWidgets.QDoubleSpinBox(self.layoutWidget)
        self.doubleSpinBox_sharp_min.setObjectName("doubleSpinBox_sharp_min")
        self.gridLayout.addWidget(self.doubleSpinBox_sharp_min, 3, 0, 1, 1)
        self.doubleSpinBox_sharp_max = QtWidgets.QDoubleSpinBox(self.layoutWidget)
        self.doubleSpinBox_sharp_max.setObjectName("doubleSpinBox_sharp_max")
        self.gridLayout.addWidget(self.doubleSpinBox_sharp_max, 3, 1, 1, 1)
        self.horizontalLayout.addLayout(self.gridLayout)
        self.line = QtWidgets.QFrame(self.layoutWidget)
        self.line.setFrameShape(QtWidgets.QFrame.VLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.horizontalLayout.addWidget(self.line)
        spacerItem1 = QtWidgets.QSpacerItem(200, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
        self.pushButton.setSizePolicy(sizePolicy)
        self.pushButton.setMinimumSize(QtCore.QSize(0, 0))
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        LayoutDemo.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(LayoutDemo)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 565, 23))
        self.menubar.setObjectName("menubar")
        LayoutDemo.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(LayoutDemo)
        self.statusbar.setObjectName("statusbar")
        LayoutDemo.setStatusBar(self.statusbar)
        self.label_5.setBuddy(self.doubleSpinBox_sharp_min)

        self.retranslateUi(LayoutDemo)
        QtCore.QMetaObject.connectSlotsByName(LayoutDemo)

    def retranslateUi(self, LayoutDemo):
        _translate = QtCore.QCoreApplication.translate
        LayoutDemo.setWindowTitle(_translate("LayoutDemo", "MainWindow"))
        self.label_3.setText(_translate("LayoutDemo", "收益"))
        self.label_4.setText(_translate("LayoutDemo", "最大回撤"))
        self.label_5.setText(_translate("LayoutDemo", "&sharp比"))
        self.label.setText(_translate("LayoutDemo", "最小值"))
        self.label_2.setText(_translate("LayoutDemo", "最大值"))
        self.pushButton.setText(_translate("LayoutDemo", "开始"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    LayoutDemo = QtWidgets.QMainWindow()
    ui = Ui_LayoutDemo()
    ui.setupUi(LayoutDemo)
    LayoutDemo.show()
    sys.exit(app.exec_())

标签:sizePolicy,记录,python,LayoutDemo,self,doubleSpinBox,pyqt5,QtWidgets,label
From: https://blog.csdn.net/baidu_24083705/article/details/140169359

相关文章

  • python-graph-study2024-7
     1.先打基础1基础学习笔记 GraphCreationNetworkXgraphobjectscanbecreatedinoneofthreeways:Graphgenerators—standardalgorithmstocreatenetworktopologies.Importingdatafrompreexisting(usuallyfile)sources.Addingedgesan......
  • python 识别图片验证码/滑块验证码准确率极高的 ddddocr 库
    前言验证码的种类有很多,它是常用的一种反爬手段,包括:图片验证码,滑块验证码,等一些常见的验证码场景。识别验证码的python库有很多,用起来也并不简单,这里推荐一个简单实用的识别验证码的库ddddocr(带带弟弟ocr)库.环境准备python版本要求小于等于python3.9版本pip安装pipin......
  • python简单操作
    一.输出九九乘法表row=1whilerow<=9:  col=1  whilecol<=row:    print("%d*%d=%d"%(row,col,row*col),end="\t")    #\t在控制台输出一个制表符,协助文本垂直方向上对齐    #\n换行符\"可输出双引号 ......
  • AtCoder Beginner Contest 361 补题记录(A~F)
    开题顺序:A-C-F-D-B-E。A直接模拟即可。boolbegmem;#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;classFastIO{public:intread(){into=1,x;charch;while(!isdigit(ch=getchar())){if......
  • Shopee虾皮api python获取虾皮购物平台的商品数据信息 数据采集
    虾皮购物(英语:Shopee)是一个电商平台,总公司设在新加坡,归属于SeaGroup(之前称之为Garena),该企业于2009年由李小冬(ForrestLi)创办。虾皮购物于2015年初次在新加坡推出,现阶段已拓展到马来西亚、泰国、印度尼西亚、越南和菲律宾。虾皮购物为全球华人地区的客户提供线上购物和销售......
  • Appium+python自动化(四十二)- 寿终正寝完结篇 - 结尾有惊喜,过时不候(超详解)
    1.简介 按照上一篇的计划,今天给小伙伴们分享执行测试用例,生成测试报告,以及自动化平台。今天这篇分享讲解完。Appium自动化测试框架就要告一段落了。2.执行测试用例&报告生成 测试报告,宏哥已经讲解了testng、HTMLTestRunner、allure等等,今天就在讲解一个新的测试报告BSTest......
  • python socket模块实现上传文件到服务器
    socket模块文件上传案例catserver.py#-*-coding:UTF-8-*-importsocketsock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)sock.bind(('0.0.0.0',8001))#127.0.0.1或查看自己局域网本地IP地址sock.listen(1)conn,addr=sock.accept()#接收......
  • python速通(函数)
    (网页的生成)牛客网的某个网页基本已经写好了,最后一步为了适应手机的尺寸,需要将高度增加一倍。为了适用于多个网页,牛牛希望你能将这个功能定义为一个函数,函数输入网页高度h,输出增加后的结果。defiheight(h):returnh*2h1,h2=map(int,input().split())print(iheight......
  • python随笔day02
    1.arg元组类型和**kwargs字典类型#元组参数:元组类型数据,对传递参数有顺序要求deftest(*args):print(f"args[0]={args[0]},args[1]={args[1]},args[2]={args[2]}")test(1,2,3)#字典参数:字典类型数据,对传递参数没有顺序要求,格式要求key=value值deftest2(**kwargs):......
  • windows下C++配置googletest过程记录
    文章目录下载googletest将googletest解压到项目目录并创建build文件夹编译googletest在项目的CMakeLists.txt文件中添加相关依赖编写测试并执行参考下载googletestReleasev1.14.0·google/googletest·GitHub将googletest解压到项目目录并创建build文件夹在g......