一、布局
在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