FANDOM


Here we will talk about and learn to program our GUI's in an event driven manner. In other words, getting our functions and methods to execute in response to the user's actions. Such as button clicks, selecting items from a collection, or a mouse click. These are class events. Widgets used to build the GUI interface act as the source of events. Each widget (derived from the QObject class) is designed to emit a signal in response to one or more events. The signal does not perform any action on its own, Instead it is 'connected' to a slot. The slot can be any callable Python function. In PyQt, conneciton between a signal and a slot can be achieved with this basic syntax:

widget.signal.connect(slot_function)

ExamplesEdit

Suppose if a function is to be called when a button is clicked. Here, the clicked signal is to be connected to a callable function :

button.clicked.connect(slot_function)

Another example of a signal in PyQt would be the textChanged signal. So for example say we have a lineEdit widget and we want to run a certain function when the text in the lineEdit is changed. We could do that in the following way :

some_line_edit.textChanged.connect(some_function)

ReferencesEdit

PyQt - Signals and Slots - Tutorials Point

Community content is available under CC-BY-SA unless otherwise noted.