简体   繁体   中英

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. 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? I'm really out of ideas at this point.

Change window.addWidget(bottom_box, 0, 1) to window.addLayout(bottom_box, 0, 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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