PyQt4使用按钮更改当前窗口内容

2024-04-25 00:41:41 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我使用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处,窗口大小320240将保持不变。这就是我要达到的目标。谢谢你抽出时间。在


Tags: textimport内容helloobjectonsysbutton
1条回答
网友
1楼 · 发布于 2024-04-25 00:41:41
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_()

相关问题 更多 >