Tkinter将键盘键绑定到按钮';python中的s命令
不!不是一个函数,而是在程序中的任何特定时刻按按钮指定的“命令”选项。我有一个代码块,按钮在其中更改其功能和外观,但我希望Enter键绑定到它,无论它调用什么函数。程序中有一些点禁用了按钮,但将enter键绑定到该点仍会激活该功能,即使按钮已禁用。必须有办法做到这一点,或者有办法绕过它。也许有一种方法可以模拟按钮点击事件。我真不敢相信没有直接的方法可以做到这一点。目前我看到的唯一选择是为每个函数和按钮保持绑定和解除绑定。它看起来太不符合pythonic了。如果绑定调用按钮的Tkinter将键盘键绑定到按钮';python中的s命令,python,tkinter,command,bind,Python,Tkinter,Command,Bind,不!不是一个函数,而是在程序中的任何特定时刻按按钮指定的“命令”选项。我有一个代码块,按钮在其中更改其功能和外观,但我希望Enter键绑定到它,无论它调用什么函数。程序中有一些点禁用了按钮,但将enter键绑定到该点仍会激活该功能,即使按钮已禁用。必须有办法做到这一点,或者有办法绕过它。也许有一种方法可以模拟按钮点击事件。我真不敢相信没有直接的方法可以做到这一点。目前我看到的唯一选择是为每个函数和按钮保持绑定和解除绑定。它看起来太不符合pythonic了。如果绑定调用按钮的invoke方法,那么
invoke
方法,那么当按钮被禁用时,它将什么也不做。如果该按钮已启用,它将执行该按钮设计的任何操作。当然,您也可以绑定到一个函数,该函数在执行任何操作之前检查按钮的状态
这里有一个人为的例子。它包含一个标记为“计数”的按钮,用于增加计数器并更新显示。有两个其他按钮用于启用或禁用第一个按钮
一旦窗口具有焦点,如果您按下返回键,它将调用该按钮。请注意,禁用按钮后,返回键不起任何作用。当您重新启用按钮时,返回键再次工作
将tkinter作为tk导入
计数=0
def count():
全局计数
计数+=1
label.configure(text=f“Count:{Count}”)
root=tk.tk()
label=tk.label(根,text=“计数:0”)
button=tk.button(root,text=“Count”,command=Count)
启用按钮(root,text=“enable Count”,command=lambda:Button.configure(state=“normal”))
禁用按钮(root,text=“禁用计数”,command=lambda:Button.configure(state=“disabled”))
标签包装(侧面=“顶部”,填充=“x”,pady=20)
按钮包(side=“left”,padx=(0,10))
禁用btn.pack(side=“right”)
启用\u btn.pack(side=“right”)
root.bind_all(“,lambda事件:button.invoke())
root.mainloop()
按钮的命令可以作为
btn = tkinter.Button(command=lambda: print("Hello!"), text="Press me for greeting")
btn.pack() # This is not required to allow invoke() to work
btn.invoke()
它调用函数。即使按钮实际上不在屏幕上,这也可以工作,如代码段所示
但是,如果按钮状态为“禁用”,则它将不起作用。在这种情况下,我建议将函数存储在单独的变量中
在下面的示例中,按钮的命令只调用属于该类的方法。这意味着您可以通过按下按钮或直接调用该方法来调用该函数,即使该按钮已被禁用、隐藏或删除。该按钮只查看函数的当前定义并调用它
import tkinter as tk
class Window(tk.Tk):
def __init__(self):
super().__init__()
self.bind("<Return>", lambda event: self.current_button_function())
self.btn = tk.Button(self, text="Press me!", command=lambda: self.current_button_function())
# command=self.current_button_function won't work since it won't update when the function updates
self.btn.pack()
self.mainloop()
def current_button_function(self):
print("This is a function. Button click is now disabled but function is still callable and enter still works")
self.btn.configure(state="disabled")
#self.current_button_function = lambda: print("This is simple lambda") # For easily returning single expression
def tmp(): # For a multi-line function
print("This is another function which works even though the button is disabled! The button is no longer on the page but the function is still callable and enter still works")
self.btn.forget()
def tmp():
print("The button is not on the page but it doesn't matter!")
#Could continue adding nested 'def' and/or 'lambda'
self.current_button_function = tmp
self.current_button_function = tmp
if __name__ == "__main__":
window = Window()
将tkinter作为tk导入
类窗口(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.bind(“,lambda事件:self.current\u按钮\u函数())
self.btn=tk.Button(self,text=“Press me!”,command=lambda:self.current\u Button\u function())
#command=self.current\u button\u函数无法工作,因为它在函数更新时不会更新
self.btn.pack()
self.mainloop()
def当前按钮功能(自身):
打印(“这是一个函数。按钮单击现在被禁用,但函数仍然可以调用,并且输入仍然有效”)
self.btn.configure(state=“disabled”)
#self.current_button_function=lambda:print(“这是简单的lambda”)#用于轻松返回单个表达式
def tmp():#对于多行函数
打印(“这是另一个即使按钮被禁用也能工作的功能!按钮不再在页面上,但该功能仍然可以调用并输入仍然有效”)
self.btn.forget()
def tmp():
打印(“按钮不在页面上,但不重要!”)
#无法继续添加嵌套的“def”和/或“lambda”
self.current\u按钮功能=tmp
self.current\u按钮功能=tmp
如果名称=“\uuuuu main\uuuuuuuu”:
window=window()
尝试单击按钮和/或按enter键几次,看看它是否会更改回调函数,即使按钮被忘记或禁用,它仍能工作。无论如何,这似乎是我唯一的办法,没有am示例,很难理解,请包含一些我们可以运行的东西。因为我还没有弄明白,所以没有代码可以包含。我现在只使用常规按钮。bind('Try
invoke()
?在主块中说global COUNT
有什么用吗?我听到很多人说它没有用。@CoolCloud:哈,没有。那是个错误。谢谢你提醒我。
import tkinter as tk
class Window(tk.Tk):
def __init__(self):
super().__init__()
self.bind("<Return>", lambda event: self.current_button_function())
self.btn = tk.Button(self, text="Press me!", command=lambda: self.current_button_function())
# command=self.current_button_function won't work since it won't update when the function updates
self.btn.pack()
self.mainloop()
def current_button_function(self):
print("This is a function. Button click is now disabled but function is still callable and enter still works")
self.btn.configure(state="disabled")
#self.current_button_function = lambda: print("This is simple lambda") # For easily returning single expression
def tmp(): # For a multi-line function
print("This is another function which works even though the button is disabled! The button is no longer on the page but the function is still callable and enter still works")
self.btn.forget()
def tmp():
print("The button is not on the page but it doesn't matter!")
#Could continue adding nested 'def' and/or 'lambda'
self.current_button_function = tmp
self.current_button_function = tmp
if __name__ == "__main__":
window = Window()