2017-03-01 150 views
-1

你好,我显然不是太有Tkinter的经验非常多,我不可能发现什么一直在寻找什么,也许有人可以帮我简单的按钮隐藏

def hide(x): 
    x.pack_forget() 

d=Button(root, text="Click to hide me!" command=hide(d)) 
d.pack() 

我想它所以当点击命令运行但按钮没有定义时调用命令

回答

1

如果您还在建筑中,则不能使用任何东西。你必须使用configurelambda功能:

from tkinter import * 

def hide(x): 
    x.pack_forget() 

root = Tk() 
d=Button(root, text="Click to hide me!") 

d.configure(command=lambda: hide(d)) 
d.pack() 
root.mainloop() 
+0

这是不完全正确的。在这种情况下不需要使用'configure'。 –

+0

你为什么这么说,解释一下自己? – eyllanesc

+0

我的错误。我没有看到您将相同的窗口小部件引用传递给该命令。 –

0

首先,定义按钮,然后添加命令与config方法。

from tkinter import * 

root = Tk() 
def hide(x): 
    x.pack_forget() 

d=Button(root, text="Click to hide me!") 
d.pack() 
d.config(command=lambda: hide(d)) 

root.mainloop()