我想我的列表框“listbox1”改变其绑定时,按钮“button1”被按下。第一次单击按钮“禁用”列表框,而不会通过listbox1.bindtags((listbox1, Listbox, ".", "all"))
丢失对列表框中所选元素的焦点。你知道吗
第二次单击应该用<<ListboxSelect>>
绑定重新绑定列表框。你知道吗
问题:如何重新绑定列表框?我试着做一个简单的listbox1.configure
,listbox1.bind
,把listbox1.bindtags
中的参数颠倒过来,用谷歌搜索,看这里,我还是搞不明白。你知道吗
from tkinter import *
root = Tk()
buttontext = StringVar()
buttontext.set("Disable")
frame_1 = Frame(root, bg="white")
frame_1.pack()
def print_(event):
print("success")
listbox_1 = Listbox(frame_1, activestyle="none", selectmode=SINGLE, height=6, width=11)
listbox_1.pack()
listbox_1.bind("<<ListboxSelect>>", print_)
listbox_1.insert(0, "test1")
listbox_1.insert(1, "test2")
def toggle_button():
if buttontext.get() == "Disable":
listbox_1.bindtags((listbox_1, Listbox, ".", "all"))
listbox_1["exportselection"] = False
buttontext.set("Normal")
elif buttontext.get() == "Normal":
listbox_1.bind("<<ListboxSelect>>", print_)
listbox_1["exportselection"] = True
buttontext.set("Disable")
button = Button(frame_1, textvariable=buttontext, command=toggle_button)
button.pack()
root.mainloop()
问题从这行代码开始:
它不是在做你认为它在做的事情。虽然它实际上禁用了小部件,但这是因为您正在用无效的标记替换有效的标记。如果要通过删除或更改默认标记来禁用小部件,更正确的方法是删除默认绑定标记:
要恢复绑定,您所要做的就是恢复绑定标记。注意我是如何使用
"Listbox"
作为字符串的,而不是实际的类:注意:您不需要重新添加绑定,只需要重新建立正确的绑定标签:
相关问题 更多 >
编程相关推荐