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

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

主站蜘蛛池模板: 保康县| 迭部县| 汤原县| 新源县| 岗巴县| 剑川县| 新安县| 牡丹江市| 芮城县| 蒲城县| 彭水| 墨玉县| 莎车县| 泉州市| 苍梧县| 章丘市| 全南县| 平乐县| 克山县| 儋州市| 平阳县| 延安市| 绿春县| 怀化市| 叶城县| 长顺县| 万全县| 京山县| 鱼台县| 河曲县| 三都| 漠河县| 长海县| 乌兰浩特市| 封开县| 肃南| 日喀则市| 海晏县| 固原市| 黔西县| 民勤县|