【问题标题】:PyQt4 Using button to change current window contentsPyQt4 使用按钮更改当前窗口内容
【发布时间】:2016-03-02 02:37:30
【问题描述】:

所以我使用 PyQt4 来设计数据仓库的一部分。我想要一个按钮来清除所有当前窗口内容并使用同一窗口将其替换为新内容。这是我的代码:

    import sys
    import ETL
    import urllib
    from PyQt4.QtGui import *

    def on_click():
        #change window contents to new contents

        # Creates text that says "Hello"
        Text = QLabel("Hello", Window)
        # Text is moved to coordinates 21, 30
        Text.move(21, 30)


    # Creates PyQt4 Application object
    App = QApplication(sys.argv)
    # Create Window object
    Window = QWidget()
    Window.resize(320, 240)

    # Creates button object called "Submit"
    Button = QPushButton('Submit', Window)
    # Moves button (Right, Down)
    Button.move(200, 180)
    # When button's clicked executes function called on_click()
    Button.clicked.connect(on_click)

    # Displays window
    Window.show()
    # Needed so the gui window stays open until user closes it
    App.exec_()

所以在按下按钮之后。该按钮将消失。文本“Hello”将出现在坐标 21,30 处,并且窗口大小 320、240 将保持不变。这就是我想要达到的目标。感谢您的宝贵时间。

【问题讨论】:

    标签: python qt pyqt qt4 pyqt4


    【解决方案1】:
    import sys
    import urllib
    from PyQt4.QtGui import *
    
    
    def on_click():
        #change window contents to new contents
    
        # Creates text that says "Hello"
        text = QLabel('Hello', Window)
        # Text is moved to coordinates 21, 30
        text.move(21, 30)
        QLabel.show(text)
        button.hide()
    
    
    
    # Creates PyQt4 Application object
    App = QApplication(sys.argv)
    # Create Window object
    Window = QWidget()
    Window.resize(320, 240)
    
    # Creates button object called "Submit"
    button = QPushButton('Submit', Window)
    # Moves button (Right, Down)
    button.move(200, 180)
    # When button's clicked executes function called on_click()
    button.clicked.connect(on_click)
    
    # Displays window
    Window.show()
    # Needed so the gui window stays open until user closes it
    App.exec_()
    

    【讨论】:

    • 感谢您的回答。我知道它必须非常简单。
    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多