- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 234字
- 2021-06-24 18:35:04
Command binding
The simplest way to add functionality to a button is called command binding, whereby a callback function is mentioned in the form of command = some_callback in the widget option. Note that the command option is available only for a few selected widgets.
Take a look at the following sample code:
def my_callback ():
# do something when button is clicked
After defining the preceding callback, we can connect it to, say, a button with the command option referring to the callback, as follows:
tk.Button(root, text="Click me", command=my_callback)
A callback is a function memory reference ( my_callback in the preceding example) that is called by another function (which is Button in the preceding example) and that takes the first function as a parameter. Put simply, a callback is a function that you provide to another function so that it can calling it.
Note that my_callback is passed without parentheses, (), from within the widget command option, because when the callback functions are set it is necessary to pass a reference to a function rather than actually call it.
If you add parentheses, (), as you would for any normal function, it would be called as soon as the program runs. In contrast, the callback is called only when an event occurs (the pressing of a button in this case).
- Android Wearable Programming
- 多媒體CAI課件設(shè)計與制作導(dǎo)論(第二版)
- Boost程序庫完全開發(fā)指南:深入C++”準(zhǔn)”標(biāo)準(zhǔn)庫(第5版)
- SoapUI Cookbook
- Elasticsearch Server(Third Edition)
- Angular開發(fā)入門與實戰(zhàn)
- Julia for Data Science
- Learning Modular Java Programming
- 響應(yīng)式Web設(shè)計:HTML5和CSS3實戰(zhàn)(第2版)
- 軟件測試綜合技術(shù)
- Emotional Intelligence for IT Professionals
- Wearable:Tech Projects with the Raspberry Pi Zero
- Web前端測試與集成:Jasmine/Selenium/Protractor/Jenkins的最佳實踐
- HTML5 and CSS3:Building Responsive Websites
- JavaScript高級程序設(shè)計(第3版)