Part 4 - Dynamically updating widgetsΒΆ
Now that we know how to show multiple widgets in a single window, we can make use of what we learned in 2.button.py to connect different widgets together. For example, we can make it so that pressing a button changes the text in one of the widgets:
from qtpy.QtWidgets import (QApplication, QLabel, QWidget, QVBoxLayout,
QPushButton)
# Initialize application
app = QApplication([])
# Create label
label = QLabel('Zzzzz')
def say_hello(event):
label.setText('Hello, world!')
# Create button
button = QPushButton('Press me!')
button.clicked.connect(say_hello)
# Create layout and add widgets
layout = QVBoxLayout()
layout.addWidget(label)
layout.addWidget(button)
# Apply layout to widget
widget = QWidget()
widget.setLayout(layout)
widget.show()
app.exec_()
Try running this script, and you should see something like:
Now press the button, and the label text should change: