首页 > 解决方案 > 按下按钮时创建按钮

问题描述

Python GUI 很新。我正在创建一个小程序,其中有一个按钮,当我按下它时,它应该在 GUI 中创建另一个按钮。

试过这个:

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

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 Pet Project'
        self.left = 10
        self.top = 10
        self.width = 1000
        self.height = 700
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,  self.top,  self.width,  self.height)

        # Create a button in the window
        self.buttonA = QPushButton('Simple If',  self)
        self.buttonA.move(10, 10)
        # connect button to function on_click
        self.buttonA.clicked.connect(self.on_click)
        self.show()

    def on_click(self):
        # Need to create a button here , only if buttonA is pressed
        self.buttonB = QPushButton('Simple If',  self)
        self.buttonB.move(200, 10)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

但它没有更新 GUI 中的任何内容。也搜索了 GUI 更新,但对我没有任何帮助。

标签: pyqt5

解决方案


推荐阅读