![](/img/trans.png)
[英]How to adjust the geometry of widgets added in vertical box layout in pyqt5
[英]How to adjust the content to the main window in pyqt5, when it's geometry is fixed or being changed?
我正在創建一個自定義控件並想要創建一行按鈕。 一開始我從兩個按鈕開始,代碼如下所示:
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)
問題是按鈕不適合窗口(因為我猜是幾何設置)。 我的目的是在運行主窗口的代碼時,例如連續顯示 20 個按鈕而不被切斷(這意味着無論按鈕的數量如何)。
如何將按鈕或窗口調整到正確的大小,或者如何將按鈕調整到主窗口或主窗口調整到內容?
如何獲得一排滿足相同要求的圓形按鈕?
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))
您需要使用布局。 一個簡單的實現可能是
class Window(QMainWindow):
def __init__(self):
super().__init__()
mainWidget = QWidget()
mainLayout = QHBoxLayout()
self.setCentralWidget(mainWidget)
mainWidget.setLayout(mainLayout)
然后像這樣打包你的按鈕
button1 = QPushButton("")
button2 = QPushButton("")
button3 = QPushButton("")
mainLayout.addWidget(button)
mainLayout.addWidget(button2)
mainLayout.addWidget(button3)
在此示例中,它們將按順序出現在水平列中,但您有多個布局管理器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.