WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 520 reviews for our classroom and online training
Should you be using QT for Python as a GUI?
Part six of an eight-part series of blogs

If you need to build a GUI system in Python, PyQt5 is probably the best choice, but don't take our word for it - read this blog and see what you think.

  1. Should you be using PyQt?
  2. QT Designer
  3. Widgets
  4. Layouts
  5. Forms and code
  6. Slots and Signals (this blog)
  7. CSS and Formatting
  8. Models and Data

Posted by Andy Brown on 21 June 2022

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Slots and Signals

To get buttons (or other widgets) to do something when you click on them or change them, you listen for the signals that they emit and assign these to slots.

To show how this works, suppose that you have a simple form containing a single label and single push button:

Simple form

We've called the label lblOwlMessage and the button btnShowOwlMessage.

 

We hide the label when the form loads (you could do this in QT Designer instead):

# hide the message to start

self.lblOwlMessage: QLabel = self.findChild(QLabel,'lblOwlMessage')

self.lblOwlMessage.setVisible(False)

Next we create a slot to handle the event of clicking on the button (in most Microsoft applications this would be called an event-handler):

def show_message(self):

# show the hidden label

self.lblOwlMessage.setVisible(True)

Finally, we connect the clicked signal from the push button to the show_message slot above:

# get push button to do something

self.btnShowOwlMessage: QPushButton = self.findChild(QPushButton,'btnShowOwlMessage')

self.btnShowOwlMessage.clicked.connect(self.show_message)

The range of signals emitted by widgets is mind-boggling, but having to tie them to slots is a bit tedious.

This blog has 0 threads Add post