python – 为什么只有一些Tkinter回调函数需要有一个参数,而其他函数则不需要
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 为什么只有一些Tkinter回调函数需要有一个参数,而其他函数则不需要,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1732字,纯文字阅读大概需要3分钟。
内容图文
![python – 为什么只有一些Tkinter回调函数需要有一个参数,而其他函数则不需要](/upload/InfoBanner/zyjiaocheng/816/d4a676bf9780418e91d8b105d55cac7f.jpg)
我正在使用Python 2.7,如果这很重要的话.
这是我为了好玩而编写的代码:
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中的一些小部件,他们的命令需要将“event”作为参数,而其他小部件则不需要.为什么?如何区分他们?
谢谢.
解决方法:
比例不会发生事件.它需要当前值.试试这个:
def cmd(value):
print int(value)
如果您阅读了Tk tutorial,它解释了这一点:
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).
或者,如果您阅读实际的manpage:
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 docs并不是那么完整 – 他们假设您已经知道Tcl / Tk如何工作,或者如何自己查找.这就是为什么文档从一系列链接到Tk文档源的链接开始的原因.
如果您希望通过反复试验来解决问题,那么看到传递的内容并不难:
def cmd(*args):
print('Scale command says {}'.format(args))
def enable(*args):
print('Button command says {}'.format(args))
但这并不总能告诉你需要知道的一切;还有其他回调,其参数不够明显,无需更多工作,或者可配置(例如,验证回调).
内容总结
以上是互联网集市为您收集整理的python – 为什么只有一些Tkinter回调函数需要有一个参数,而其他函数则不需要全部内容,希望文章能够帮你解决python – 为什么只有一些Tkinter回调函数需要有一个参数,而其他函数则不需要所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。