简体   繁体   English

如何使用 PyQt5 将框添加到网格布局

[英]How to add boxes to a grid layout with PyQt5

I'm trying to make a grid layout where the top is message box and the bottom a horizontal box with 3 buttons.我正在尝试制作一个网格布局,其中顶部是消息框,底部是带有 3 个按钮的水平框。 The code I'm using is this:我正在使用的代码是这样的:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import * 

def on_button_clicked(b):
    print(b.text(), "was pressed.")


app = QApplication(sys.argv)
w = QWidget()

button1 = QPushButton("First")
button1.clicked.connect(lambda: on_button_clicked(button1))

button2 = QPushButton("Second")
button2.clicked.connect(lambda: on_button_clicked(button2))

button3 = QPushButton("Third")
button3.clicked.connect(lambda: on_button_clicked(button3))

top_box = QMessageBox()

bottom_box = QHBoxLayout()
bottom_box.addWidget(button1)
bottom_box.addWidget(button2)
bottom_box.addWidget(button3)

window = QGridLayout()
window.addWidget(top_box, 0, 0)
window.addWidget(bottom_box, 0, 1)

w.setLayout(window)
w.show()
sys.exit(app.exec_())

No matter what though, it always outputs the following problem:不管怎样,它总是输出以下问题:

  addWidget(self, QWidget): argument 1 has unexpected type 'QHBoxLayout'
  addWidget(self, QWidget, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'QHBoxLayout'
  addWidget(self, QWidget, int, int, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'QHBoxLayout'

Why it doesn't work?为什么它不起作用? How can I make it work?我怎样才能让它发挥作用? Is it something basic about PyQt functionality that I'm just not getting or what?它是关于 PyQt 功能的一些基本功能,我只是没有得到还是什么? I'm really out of ideas at this point.在这一点上我真的没主意了。

Change window.addWidget(bottom_box, 0, 1) to window.addLayout(bottom_box, 0, 1)将 window.addWidget(bottom_box, 0, 1) 更改为 window.addLayout(bottom_box, 0, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM