[英]How to adjust the content to the main window in pyqt5, when it's geometry is fixed or being changed?
I am creating a custom control and want to create a row of buttons.我正在创建一个自定义控件并想要创建一行按钮。 For the beginning I started with two buttons, the code looks like that:
一开始我从两个按钮开始,代码如下所示:
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Python ")
self.setGeometry(500, 600, 200, 200)
self.UiComponents()
self.show()
def UiComponents(self):
button1 = QPushButton( "", self)
button2 = QPushButton( "", self)
button1.setGeometry(10, 150, 100, 100)
button2.setGeometry(150, 150, 100, 100)
The problem is that the buttons don't fit into the window (because of the geometry setting I guess).问题是按钮不适合窗口(因为我猜是几何设置)。 My purpose is when running the code that the main window shows for example 20 buttons in a row without being cut off (meaning regardless of the number of buttons).
我的目的是在运行主窗口的代码时,例如连续显示 20 个按钮而不被切断(这意味着无论按钮的数量如何)。
How do I get the buttons or window to the correct size or how can I adjust the buttons to the main window or the main window to the content?如何将按钮或窗口调整到正确的大小,或者如何将按钮调整到主窗口或主窗口调整到内容?
How do I get a row of circular buttons that meet the same requirements?如何获得一排满足相同要求的圆形按钮?
def Function(self):
xpos = 10
for i in range(5):
button = QPushButton("".format(i+1), self)
button.setGeometry(xpos, 150, 50, 50 )
xpos = xpos + 50
button.setStyleSheet("border : 1px solid black;background-color : green; border-radius : 25px")
button.clicked.connect(lambda ch, i=i+1: self.function(i))
button.clicked.connect(lambda ch, i=button : self.color(i))
You need to use a layout.您需要使用布局。 A simple implementation of one might be
一个简单的实现可能是
class Window(QMainWindow):
def __init__(self):
super().__init__()
mainWidget = QWidget()
mainLayout = QHBoxLayout()
self.setCentralWidget(mainWidget)
mainWidget.setLayout(mainLayout)
and then pack your buttons in like然后像这样打包你的按钮
button1 = QPushButton("")
button2 = QPushButton("")
button3 = QPushButton("")
mainLayout.addWidget(button)
mainLayout.addWidget(button2)
mainLayout.addWidget(button3)
In this example, they would appear in order in a horizontal column, but you there are several layout managers .在此示例中,它们将按顺序出现在水平列中,但您有多个布局管理器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.