- 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).
- DevOps:軟件架構(gòu)師行動指南
- Vue 3移動Web開發(fā)與性能調(diào)優(yōu)實戰(zhàn)
- 流量的秘密:Google Analytics網(wǎng)站分析與優(yōu)化技巧(第2版)
- Software Defined Networking with OpenFlow
- C語言程序設(shè)計案例教程(第2版)
- Python Geospatial Development(Second Edition)
- Getting Started with SQL Server 2012 Cube Development
- Oracle Exadata專家手冊
- 從0到1:Python數(shù)據(jù)分析
- Java:High-Performance Apps with Java 9
- 計算機應(yīng)用基礎(chǔ)教程(Windows 7+Office 2010)
- Apache Camel Developer's Cookbook
- Modern C++ Programming Cookbook
- 進入IT企業(yè)必讀的324個Java面試題
- Applied Deep Learning with Python