【发布时间】:2017-01-05 11:17:45
【问题描述】:
我希望我的列表框“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()
【问题讨论】:
-
这段代码不会运行,它有很多错误。
buttntxt未定义,do_smth未定义,第 18 行存在语法错误,buttntxt.get()不是有效语句,您不能提供类作为绑定标签,... -
它与环境无关,我知道此代码不会运行,但我的实际代码可以。我的问题只是如何重新绑定小部件。 btw buttntxt 和 buttontxt 是同一个变量,是拼写错误
-
要重新绑定一个小部件,您只需调用
bind方法。既然你声称你已经尝试过但没有成功,我们需要看看你实际尝试了什么,而不是一些假冒的代码。 -
原始代码有481行,我不知道这样会不会太混乱。 :( 基本上我正在做的是在 if 子句中告诉 "listbox1.bindtags((listbox1, Listbox, ".", "all"))",在 elif 子句中是 "listbox1.bind(">", 函数1)"
-
@tumper 然后请查看help center,更具体地说是关于编写Minimal, Complete, and Verifiable Example (MCVE) 的部分。
标签: python python-3.x tkinter listbox bind