[英]Python PyQt5 I wanna set the position of my Grid Layout
I need to set the position of my Grid Layout.我需要设置网格布局的 position。 It has to be exactly where I want it to be.
它必须恰好在我想要的位置。 I've tried some methods like ".setGeometry, .setAlignment" and I couldn't unfortunately have the result exactly I wanted.
我尝试了一些方法,例如“.setGeometry,.setAlignment”,但不幸的是我无法得到我想要的结果。
As you can see from this photo, I want it to be in the bottom right but I guess there's no method for it.正如你从这张照片中看到的,我希望它位于右下角,但我想没有办法。 (I need to say that the interface in the photo below is a different interface. I don't need this interface. Because of I wanted to show you exactly what kind of stuff I wanted.)
(不得不说下图中的界面是不同的界面,我不需要这个界面,因为我想告诉你我到底想要什么样的东西。)
You can achieve this using layouts and their setStretch
methods.您可以使用布局及其
setStretch
方法来实现此目的。
Here is a runnable example:这是一个可运行的示例:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MainWindow(QMainWindow):
def __init__(self, parent=None) -> None:
super().__init__(parent)
self.central = QWidget()
self.layout = QVBoxLayout(self.central)
self.hlayout = QHBoxLayout()
self.btn1 = QPushButton("PushButton", self)
self.btn2 = QPushButton("PushButton", self)
self.btn3 = QPushButton("PushButton", self)
self.hlayout.addStretch()
self.hlayout.addWidget(self.btn1)
self.hlayout.addWidget(self.btn2)
self.hlayout.addWidget(self.btn3)
self.layout.addStretch()
self.layout.addLayout(self.hlayout)
self.setCentralWidget(self.central)
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
The alternative would be to use the QHBoxLayout.setgeometry(QRect(x, y, w, h))
but this will force the layout into a specific position that will not adjust dynamically if you resize the window, or for any other reason.另一种方法是使用
QHBoxLayout.setgeometry(QRect(x, y, w, h))
但这将强制布局进入特定的 position,如果您调整 window 的大小或出于任何其他原因,该布局将不会动态调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.