我可以在 init 函数中从布局的网格中添加一个小部件,但事实上,我只能将小部件添加到布局中。
这是怎么回事?
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QGridLayout
class MyDialog(QDialog):
def __init__(self):
super(MyDialog, self).__init__()
self.resize(600, 400)
self.setLayout(QGridLayout())
self.button_1 = QPushButton(self, text="First!")
self.button_1.setGeometry(10, 30, 150, 50)
self.button_2 = QPushButton(self, text="Add Another!")
self.button_2.setGeometry(30, 50, 150, 50)
self.button_1.clicked.connect(self.add_button)
self.button_2.clicked.connect(self.add_button)
def add_button(self):
self.button_3 = QPushButton(self, text="New!")
# doesn't work
# self.button_3.setGeometry(50, 70, 150, 50)
# can only add on layout
self.layout().addWidget(self.button_3, 2, 1, 1, 1)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyDialog()
window.show()
app.exec_()