- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 313字
- 2021-06-24 18:35:05
Binding levels
In the previous section, you had a look at how to bind an event to an instance of a widget. This can be called an instance-level binding.
However, there may be times when you need to bind events to an entire application. At times, you may want to bind an event to a particular class of widget. Tkinter provides the following levels of binding options for this:
- Application-level binding: Application-level bindings let you use the same binding across all windows and widgets of an application as long as any one window of the application is in focus. The syntax for application-level bindings is as follows:
widget.bind_all(event, callback, add=None)
The typical usage pattern is as follows:
root.bind_all('<F1>', show_help)
Application-level binding here means that, irrespective of the widget that is currently under focus, pressing the F1 key will always trigger the show_help callback as long as the application is in focus.
- Class-level binding: You can also bind events at a particular class level. This is normally used to set the same behavior for all instances of a particular widget class. The syntax for class-level binding is as follows:
w.bind_class(class_name, event, callback, add=None)
The typical usage pattern is as follows:
my_entry.bind_class('Entry', '<Control-V>', paste)
In the preceding example, all the entry widgets will be bound to the <Control-V> event, which will call a method named paste (event).
Most keyboard and mouse events occur at the operating system level. The event propagates hierarchically upward from its source until it finds a window that has the corresponding binding. The event propagation does not stop there. It propagates itself upwards, looking for other bindings from other widgets, until it reaches the root window. If it does reach the root window and no bindings are discovered by it, the event is disregarded.
- 深度實(shí)踐OpenStack:基于Python的OpenStack組件開發(fā)
- Spring Boot開發(fā)與測試實(shí)戰(zhàn)
- Oracle 11g從入門到精通(第2版) (軟件開發(fā)視頻大講堂)
- SpringMVC+MyBatis快速開發(fā)與項(xiàng)目實(shí)戰(zhàn)
- C# 2012程序設(shè)計(jì)實(shí)踐教程 (清華電腦學(xué)堂)
- Java高手真經(jīng)(高級編程卷):Java Web高級開發(fā)技術(shù)
- 算法大爆炸:面試通關(guān)步步為營
- EPLAN實(shí)戰(zhàn)設(shè)計(jì)
- Learning Concurrency in Kotlin
- JavaScript應(yīng)用開發(fā)實(shí)踐指南
- 監(jiān)控的藝術(shù):云原生時(shí)代的監(jiān)控框架
- Training Systems Using Python Statistical Modeling
- Mastering Concurrency in Python
- UML軟件建模
- 計(jì)算語言學(xué)導(dǎo)論