我试图通过按下另一个QPush按钮来弄清楚如何创建QPushButton,这样我最终可以动态创建按钮。看起来创建按钮的初始方法在函数中不起作用。
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(300, 200))
pybutton = QPushButton('Create a button', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(100,100)
pybutton.move(100, 100)
def clickMethod(self):
print('Clicked')
newBtn = QPushButton('New Button', self)
newBtn.move(0, 0)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )