为什么只有一些Tkinter回调函数需要有一个参数,而另一些则不需要 [英] Why only some Tkinter callback functions need to have an argument but others don't

查看:70
本文介绍了为什么只有一些Tkinter回调函数需要有一个参数,而另一些则不需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要这样做,我正在使用Python 2.7.

I'm using Python 2.7, if that matters.

这是我写的一个有趣的代码:

Here is a code I wrote for fun:

def p():
    root = Tk()

    def cmd(event):
        print int(slider.get())

    slider = Scale(root, orient = "horizontal", from_ = 0, to = 100, command = cmd, state = "disabled")

    def enable():
        slider.config(state = "active")

    b = Button(root, text = "Enable slider", command = enable)

    b.grid()
    slider.grid(row = 1)

    root.mainloop()

对于此代码,我想知道为什么Scale的命令需要一个事件,而Button的却不需要.看来,对于Tkinter中的某些小部件,其命令需要将事件"作为参数,而其他命令则不需要.为什么?如何区分它们?

For this code, I'm wondering why the command for Scale requires an event, but that for Button does not. It seems that for some widgets in Tkinter their commands need to have "event" as an argument, and other don't. Why? How to distinguish them?

谢谢.

推荐答案

Scale 参加活动.它采用当前值.试试这个:

Scale doesn't take an event. It takes the current value. Try this:

def cmd(value):
    print int(value)

如果您阅读了 Tk教程,则说明了这一点:

If you read the Tk tutorial, it explains this:

有一个"command" 配置选项,该选项可让您指定在更改刻度时要调用的脚本.每次调用Tk时,Tk都会自动将比例的当前值附加为参数(我们看到了类似的事情,在滚动条回调及其滚动的小部件上添加了额外的参数).

There is a "command" configuration option, which lets you specify a script to call whenever the scale is changed. Tk will automatically append the current value of the scale as a parameter each time it invokes this script (we saw a similar thing with extra parameters being added to scrollbar callbacks and those on the widgets they scroll).

或者,如果您阅读了实际的联机帮助页:

Or, if you read the actual manpage:

指定Tcl命令的前缀,只要通过小部件命令更改刻度的值即可调用.实际命令由该选项组成,后跟一个空格和一个实数,表示刻度的新值.

Specifies the prefix of a Tcl command to invoke whenever the scale's value is changed via a widget command. The actual command consists of this option followed by a space and a real number indicating the new value of the scale.

换句话说,区分它们的方法是阅读文档.不幸的是, Tkinter文档并不完整,它们假设您已经知道Tcl/Tk的工作方式,或如何自行查找.这就是为什么文档从Tk文档来源的链接列表开始的原因.

In other words, the way to distinguish them is to read the docs. Unfortunately, the Tkinter docs aren't all that complete—they assume you already know how Tcl/Tk works, or how to look it up yourself. That's why the docs start off with a list of links to sources of Tk documentation.

如果您想通过反复试验找出答案,那么查看通过的内容并不难:

If you prefer to figure it out by trial and error, it's not that hard to see what gets passed:

def cmd(*args):
    print('Scale command says {}'.format(args))

def enable(*args):
    print('Button command says {}'.format(args))

但这并不总是告诉您所有您需要了解的内容;还有其他一些回调函数,它们的参数不够明显,无法进行大量工作,或者是可配置的(例如validate回调函数).

But this won't always tell you everything you need to know; there are other callbacks whose arguments aren't obvious enough to figure out without a lot more work, or which are configurable (e.g., the validate callback).

这篇关于为什么只有一些Tkinter回调函数需要有一个参数,而另一些则不需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆