Callback

At this point, we have created many widgets and layouts. Now we need to give a widget a job to do, by which I mean what we want a button to do when it is being clicked. Currently, a button would not do anything if it is clicked.

Let's create a simple script to explain how we can give a callback to a button for the clicking event. Name it hello_connect_simple.py.

The script contains the following lines of code:

import sysfrom PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabelfrom PySide2 import QtCoreapp = QApplication(sys.argv)hello_button = QPushButton("Hello")world_label = QLabel("Sun")layout = QVBoxLayout()layout.addWidget(hello_button)layout.addWidget(world_label)def set_text_in_world_label(): world_label.setText("World") ...

Get Hands-On Blockchain for Python Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.