【问题标题】:PyQt: Add qpushbutton dynamicallyPyQt:动态添加 qpushbutton
【发布时间】:2018-10-01 14:09:52
【问题描述】:

我试图弄清楚如何通过按下另一个 QPushbutton 来创建 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_() )

【问题讨论】:

    标签: python dynamic pyqt pyqt5 qpushbutton


    【解决方案1】:

    当一个窗口被显示时,它会调用其子窗口的show() 方法,因此子窗口是可见的。如果您希望之后添加的按钮可见,则必须调用按钮的 show() 方法

    def clickMethod(self):
        print('Clicked')
        newBtn = QPushButton('New Button', self)
        newBtn.move(0, 0)
        newBtn.show()
    

    【讨论】:

      猜你喜欢
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 2017-12-19
      • 1970-01-01
      • 2014-01-07
      • 2016-11-13
      • 1970-01-01
      相关资源
      最近更新 更多