tkinter:解绑不起作用,甚至解决方法也不起作用



我在这里提出了一个问题,这个问题已经存在了很长时间,但显然仍未解决,而且旧的解决方案在我的Python 3.7.2(Win10上的64位(上不起作用
我有这个代码:

import tkinter as tk
import tkinter.simpledialog
# message box to enter a value where to set the scale to
class EnterValueBox(tk.simpledialog.Dialog):
def body(self, master):
self.e = tk.Entry(self, width=10)
self.e.pack(pady=5)
return self.e  # initial focus
def apply(self):
print(self.e.get())

# callback to open message box
def enterValue(event):
EnterValueBox(root, title="Enter Value 0..100")

# create window with scale widget
root = tk.Tk()
scale = tk.Scale(root, orient=tk.HORIZONTAL, from_=0, to=100)
scale.pack()
# unbind any button-3 events
scale.unbind("<ButtonPress-3>")
scale.unbind("<ButtonRelease-3>")
scale.unbind("<Button-3>")
# bind button-3 press event to open message box
scale.bind("<ButtonPress-3>", enterValue)
tk.mainloop()

它创建了一个带有单个缩放小部件的窗口。我想绑定ButtonPress-3打开一个小对话框,直接输入一个新值。代码只将该值打印到shell,但示例显示,取消绑定不起作用,因为打印值后,对话框将关闭(当用户单击"确定"时(,然后执行默认绑定,这将设置滑块,用户在滑块小部件的槽中单击该滑块

我尝试了使用PatchedScale小部件(而不是显示的PatchedCanvas(删除和更改tkinter事件绑定的解决方法,但没有任何区别

如有任何帮助,我们将不胜感激!

默认绑定不在小部件上,而是在小部件类上。在没有小部件特定绑定的小部件上调用unbind不会有任何效果。

如果您不希望默认绑定在小部件特定的绑定之后运行,那么通常的技术是让绑定函数返回字符串break

def enterValue(event):
EnterValueBox(root, title="Enter Value 0..100")
return "break"

相关内容

最新更新