Part 2 - Buttons and events

Let’s be honest - the application we wrote in Part 1 - Hello, World! wasn’t the most exciting one ever. In this section, we’re going to look at how to use buttons, and we will learn about events in Qt.

First, let’s create a button (remember that as mentioned in Part 1 - Hello, World!, you should always set up a QApplication first):

from qtpy.QtWidgets import QPushButton
button = QPushButton('Say hello')

Qt widgets often have different kinds of events you can connect to. An event is just something that happens for example (but not limited to) something the user does. For instance, button has an event called clicked that gets triggered when the user clicks on the button. The normal way to use events is to write functions that can then do something as a result of the event being triggered.

Let’s write a function that will get executed when the user clicks on the button:

def say_hello(event):
    print('Hello, world!')

Such a function typically takes the event as its only positional argument.

We can then connect this function to the clicked event:

button.clicked.connect(say_hello)

Finally, we can show the button with:

button.show()

Let’s take a look at the full example:

from qtpy.QtWidgets import QApplication, QPushButton

# Initialize application
app = QApplication([])

# Define a function to connect to the button
def say_hello(event):
    print('Hello, world!')

# Create button widget and connect to 'say_hello'
button = QPushButton('Say hello')
button.clicked.connect(say_hello)
button.show()

# Start 'event loop'
app.exec_()

Copy this into a Python script, and run it with e.g.:

python 2.button.py

You should see a small window pop up with a button inside:

_images/2.button.png

Now click on the button and you should see the following message appear in the terminal:

Hello, world!

Such fun!