MyTetra Share
Делитесь знаниями!
Main loop and Signals
Время создания: 01.05.2017 00:58
Раздел: Python - PyGTK - Basics
Запись: xintrea/mytetra_db_mcold/master/base/1493589538e7dy9uqnk1/text.html на raw.githubusercontent.com

Like most GUI toolkits, GTK+ uses an event-driven programming model. When the user is doing nothing, GTK+ sits in the main loop and waits for input. If the user performs some action - say, a mouse click - then the main loop “wakes up” and delivers an event to GTK+.

When widgets receive an event, they frequently emit one or more signals. Signals notify your program that “something interesting happened” by invoking functions you’ve connected to the signal. Such functions are commonly known as callbacks. When your callbacks are invoked, you would typically take some action - for example, when an Open button is clicked you might display a file chooser dialog. After a callback finishes, GTK+ will return to the main loop and await more user input.

A generic example is:

handler_id = widget.connect("event", callback, data) # событие, функция, аргументы

Firstly, widget is an instance of a widget we created earlier. Next, the event we are interested in. Each widget has its own particular events which can occur. For instance, if you have a button you usually want to connect to the “clicked” event. This means that when the button is clicked, the signal is issued. Thirdly, the callback argument is the name of the callback function. It contains the code which runs when signals of the specified type are issued. Finally, the data argument includes any data which should be passed when the signal is issued. However, this argument is completely optional and can be left out if not required.

The function returns a number that identifies this particular signal-callback pair. It is required to disconnect from a signal such that the callback function will not be called during any future or currently ongoing emissions of the signal it has been connected to.

widget.disconnect(handler_id)

If you have lost the “handler_id” for some reason (for example the handlers were installed using Gtk.Builder.connect_signals()), you can still disconnect a specific callback using the function disconnect_by_func():

widget.disconnect_by_func(callback)

Almost all applications will connect to the “delete-event” signal of the top-level window. It is emitted if a user requests that a toplevel window is closed. The default handler for this signal destroys the window, but does not terminate the application. Connecting the “delete-event” signal to the function Gtk.main_quit() will result in the desired behaviour.

window.connect("delete-event", Gtk.main_quit)

Calling Gtk.main_quit() makes the main loop inside of Gtk.main() return.

Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования