鱼C论坛

 找回密码
 立即注册
查看: 3552|回复: 5

关于Tkinter的command选项疑惑求解

[复制链接]
发表于 2016-4-17 00:23:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
小甲鱼老师的视频里Button组件里的command选项调用的函数是没有参数的,像这样
b = Button(master, text="确定", command=callback)

但是如果我的command调用的函数是带参数的,例如:
from tkinter import *

master = Tk()
a = 1
def callback(a):
    print(a+1)

b = Button(master, text="确定", command=callback(a))
b.pack()

mainloop()

在我点击确定按钮之前,就已经打印出数字 2 了,再点击确定按钮之后就没有反应了。求问这种问题应该怎么解决
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-4-17 21:41:30 | 显示全部楼层
from tkinter import *

master = Tk()
a = 1
def callback(a):
    print(a+1)

b = Button(master, text="确定", command=lambda: callback(2))
b.pack()

mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-17 22:04:16 | 显示全部楼层
from tkinter import *

master = Tk()
a=[0]

def callback(num):
    temp=a[0]
    a[0]=num+temp
    print (a[0])
   

b = Button(master, text="确定", command=lambda: callback(1))
b.pack()

mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-17 22:05:27 | 显示全部楼层
Passing Argument to Callbacks

Tkinter’s Button widget doesn’t pass any information to the callback. This makes things a bit complicated if you want to use the same callback for several buttons, like in this example:

def callback():
    print "button", "?"

Button(text="one",   command=callback).pack()
Button(text="two",   command=callback).pack()
Button(text="three", command=callback).pack()
A common beginner’s mistake is to call the callback function when constructing the widget. That is, instead of giving just the function’s name (e.g. “callback”), the programmer adds parentheses and argument values to the function:

def callback(number):
    print "button", number

Button(text="one",   command=callback(1)).pack()
Button(text="two",   command=callback(2)).pack()
Button(text="three", command=callback(3)).pack()
If you do this, Python will call the callback function before creating the widget, and pass the function’s return value to Tkinter. Tkinter then attempts to convert the return value to a string, and tells Tk to call a function with that name when the button is activated. This is probably not what you wanted.

For simple cases like this, you can use a lambda expression as a link between Tkinter and the callback function:

def callback(number):
    print "button", number

Button(text="one",   command=lambda: callback(1)).pack()
Button(text="two",   command=lambda: callback(2)).pack()
Button(text="three", command=lambda: callback(3)).pack()
To be continued.

Scoping rules, binding to variable names or values, creating multiple function objects for the same function, etc.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-9 14:35:52 | 显示全部楼层
command=callback(a)换成command=callback就是点击之后才会执行这个函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-12-18 15:15:58 | 显示全部楼层
gopythoner 发表于 2017-2-9 14:35
command=callback(a)换成command=callback就是点击之后才会执行这个函数

不传参数会报错哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-2 23:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表