Python Slots

last modified July 16, 2020

  1. Python Slots Pickle
  2. Python Slots Signals

Very simple and intuitive. For more information on Python decorators, you might want to checkout the article - Python Decorators Overview to familiarise yourself. Finally, let's instantiate a Circle, hook up the signals to the slots, and move and resize it. Monty Python’s Spamalot proved to be an immensely popular online slots machine for Playtech when it was released several years ago.This progressive jackpot is now joined by a new Monty Python slot machine game and one which is based on the popular Holy Grail film. 使用slots 但是,如果我们想要限制class的属性怎么办?比如,只允许对Student实例添加name和age属性。 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的slots变量,来限制该class能添加的属性: class Student(object).

We mentioned in the beginning that slots are preventing a waste of space with objects. Since Python 3.3 this advantage is not as impressive any more. With Python 3.3 Key-Sharing Dictionaries are used for the storage of objects. Traditional syntax: SIGNAL and SLOT QtCore.SIGNAL and QtCore.SLOT macros allow Python to interface with Qt signal and slot delivery mechanisms. This is the old way of using signals and slots. The example below uses the well known clicked signal from a QPushButton.

In this part of the PyQt5 programming tutorial, we explore events and signalsoccurring in applications.

Events in PyQt5

GUI applications are event-driven. Events are generated mainly by theuser of an application. But they can be generated by other means as well; e.g. anInternet connection, a window manager, or a timer.When we call the application's exec_() method, the application entersthe main loop. The main loop fetches events and sends them to the objects.

In the event model, there are three participants:

SlotsSlots
  • event source
  • event object
  • event target

The event source is the object whose state changes. It generates events.The event object (event) encapsulates the state changes in the event source.The event target is the object that wants to be notified. Event source objectdelegates the task of handling an event to the event target.

PyQt5 has a unique signal and slot mechanism to deal with events.Signals and slots are used for communication between objects. A signalis emitted when a particular event occurs. A slot can be any Python callable.A slot is called when its connected signal is emitted.

PyQt5 signals and slots

This is a simple example demonstrating signals and slots in PyQt5.

signals_slots.py

In our example, we display a QtGui.QLCDNumberand a QtGui.QSlider. We change the lcdnumber by dragging the slider knob.

Here we connect a valueChanged signal of the slider to thedisplay slot of the lcd number.

The sender is an object that sends a signal. The receiveris the object that receives the signal. The slot is the method thatreacts to the signal.

PyQt5 reimplementing event handler

Events in PyQt5 are processed often by reimplementing event handlers.

Python Slots Pickle

In our example, we reimplement the keyPressEvent() event handler.

If we click the Escape button, the application terminates.

Event object in PyQt5

Event object is a Python object that contains a number of attributesdescribing the event. Event object is specific to the generated eventtype.

event_object.py

In this example, we display the x and ycoordinates of a mouse pointer in a label widget.

The x and y coordinates are displayd in a QLabelwidget.

Mouse tracking is disabled by default, so the widget only receives mouse moveevents when at least one mouse button is pressed while the mouse is being moved.If mouse tracking is enabled, the widget receives mouse move events evenif no buttons are pressed.

The e is the event object; it contains data about the eventthat was triggered; in our case, a mouse move event. With the x()and y() methods we determine the x and y coordinates ofthe mouse pointer. We build the string and set it to the label widget.

PyQt5 event sender

Sometimes it is convenient to know which widget is the sender of a signal.For this, PyQt5 has the sender method.

event_sender.py

We have two buttons in our example. In the buttonClicked methodwe determine which button we have clicked by calling thesender() method.

Both buttons are connected to the same slot.

We determine the signal source by calling the sender() method.In the statusbar of the application, we show the labelof the button being pressed.

PyQt5 emitting signals

Objects created from a QObject can emit signals.The following example shows how we to emit custom signals.

We create a new signal called closeApp. This signal isemitted during a mouse press event. The signal is connected to theclose() slot of the QMainWindow.

A signal is created with the pyqtSignal() as a class attributeof the external Communicate class.

The custom closeApp signal is connected to the close()slot of the QMainWindow.

When we click on the window with a mouse pointer, the closeApp signalis emitted. The application terminates.

In this part of the PyQt5 tutorial, we have covered signals and slots.

Previous Chapter: OOP, Inheritance Example
Next Chapter: Classes and Class Creation

Slots

Python slots vs namedtuple

Avoiding Dynamically Created Attributes

The attributes of objects are stored in a dictionary __dict__. Like any other dictionary, a dictionary used for attribute storage doesn't have a fixed number of elements. In other words, you can add elements to dictionaries after they are defined, as we have seen in our chapter on dictionaries. This is the reason, why you can dynamically add attributes to objects of classes that we have created so far:

Python Slots Signals

The dictionary containing the attributes of 'a' can be accessed like this:

You might have wondered that you can dynamically add attributes to the classes, we have defined so far, but that you can't do this with built-in classes like 'int', or 'list':

Using a dictionary for attribute storage is very convenient, but it can mean a waste of space for objects, which have only a small amount of instance variables. The space consumption can become critical when creating large numbers of instances. Slots are a nice way to work around this space consumption problem. Instead of having a dynamic dict that allows adding attributes to objects dynamically, slots provide a static structure which prohibits additions after the creation of an instance.

When we design a class, we can use slots to prevent the dynamic creation of attributes. To define slots, you have to define a list with the name __slots__. The list has to contain all the attributes, you want to use. We demonstrate this in the following class, in which the slots list contains only the name for an attribute 'val'.

If we start this program, we can see, that it is not possible to create dynamically a new attribute. We fail to create an attribute 'new'.

We mentioned in the beginning that slots are preventing a waste of space with objects. Since Python 3.3 this advantage is not as impressive any more. With Python 3.3 Key-Sharing Dictionaries are used for the storage of objects. The attributes of the instances are capable of sharing part of their internal storage between each other, i.e. the part which stores the keys and their corresponding hashes. This helps reducing the memory consumption of programs, which create many instances of non-builtin types.

Previous Chapter: OOP, Inheritance Example
Next Chapter: Classes and Class Creation