官术网_书友最值得收藏!

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).

主站蜘蛛池模板: 云霄县| 镇宁| 苏尼特右旗| 南丰县| 高陵县| 安新县| 大埔区| 桓仁| 军事| 临漳县| 新巴尔虎左旗| 五莲县| 天台县| 德化县| 大城县| 于田县| 南岸区| 尚志市| 河曲县| 关岭| 东安县| 平乡县| 噶尔县| 中江县| 依兰县| 阿克苏市| 深水埗区| 五莲县| 滦平县| 邵东县| 阿拉尔市| 仪征市| 白朗县| 额尔古纳市| 湟中县| 临安市| 海口市| 清苑县| 盐津县| 临洮县| 五家渠市|