为什么我的选择回调会在我的第二个列表框上被调用?
Why does my selection callback get called on my second Listbox?
from tkinter import *
from tkinter.ttk import *
root = Tk()
listbox = None
listboxMultiple = None
listboxStr = None
listboxMultipleStr = None
def main():
global root
global listboxStr
global listboxMultipleStr
global listbox
global listboxMultiple
root.protocol("WM_DELETE_WINDOW", exitApplication)
root.title("Title Name")
root.option_add('*tearOff', False) # don't allow tear-off menus
root.geometry('1600x300')
listboxStr = StringVar()
listboxStr.set("ABCD")
listbox = Listbox(root, name="lb1", listvariable=listboxStr, width=120)
listbox.pack(side=LEFT)
listbox.bind("<<ListboxSelect>>", selectListItemCallback)
listboxMultipleStr = StringVar()
listboxMultipleStr.set("")
listboxMultiple = Listbox(root, name="lb2", listvariable=listboxMultipleStr, width=120)
listboxMultiple.pack(side=LEFT)
root.mainloop()
def selectListItemCallback(event):
global listboxMultipleStr
global listbox
global listboxMultiple
print("event.widget is {} and listbox is {} and listboxMultiple is {}\n".format(event.widget, listbox, listboxMultiple))
selection = event.widget.curselection()
listboxMultipleStr.set("")
if selection:
index = selection[0]
data = event.widget.get(index)
newvalue = "{}\n{}".format(data,"SOMETHING")
print("selected \"{}\"\n".format( data ))
print("newvalue is \"{}\"\n".format( newvalue ))
listboxMultiple.insert(END, "{}".format(data))
listboxMultiple.insert(END, "SOMETHING")
#listboxMultipleStr.set( newvalue )
else:
pass
def exitApplication():
global root
root.destroy()
if __name__ == "__main__":
main()
在 Windows 上使用 python3 7. 我为我的两个列表框小部件之一设置了回调“selectListItemCallback”。然而,当我单击“lb1”中的文本时,它按预期工作,我用相同的 selected 文本更新了“lb2”,并在“lb2”中添加了另一行。
问题是,当我 select“lb2”中的项目时,它仍然调用回调并且 event.widget 是“lb1”而不是“lb2”。
我的意图是在 'lb1' 中有一个项目列表,当我 select 其中任何一个时,'lb2' 就会充满与 select 相关的信息ed 'lb1' 项目。而且,我不想在我的 'lb2' 小部件中调用 selection 回调。
你能看出我做错了什么可能导致这种奇怪的行为吗?
谢谢。
我已经发布了代码;这在我的 Windows 7 机器上 运行 使用 python3。 Python 准确地说是 3.8.6。
这是因为当您单击另一个列表框时第一个列表框失去选择时会触发该事件。只要选择发生变化,事件就会触发,而不仅仅是在设置时触发。
如果您不希望在单击第二个列表框时第一个列表框丢失其选择,请将列表框的 exportselection
设置为 False
。否则,tkinter 将一次只允许一个选择。
from tkinter import *
from tkinter.ttk import *
root = Tk()
listbox = None
listboxMultiple = None
listboxStr = None
listboxMultipleStr = None
def main():
global root
global listboxStr
global listboxMultipleStr
global listbox
global listboxMultiple
root.protocol("WM_DELETE_WINDOW", exitApplication)
root.title("Title Name")
root.option_add('*tearOff', False) # don't allow tear-off menus
root.geometry('1600x300')
listboxStr = StringVar()
listboxStr.set("ABCD")
listbox = Listbox(root, name="lb1", listvariable=listboxStr, width=120)
listbox.pack(side=LEFT)
listbox.bind("<<ListboxSelect>>", selectListItemCallback)
listboxMultipleStr = StringVar()
listboxMultipleStr.set("")
listboxMultiple = Listbox(root, name="lb2", listvariable=listboxMultipleStr, width=120)
listboxMultiple.pack(side=LEFT)
root.mainloop()
def selectListItemCallback(event):
global listboxMultipleStr
global listbox
global listboxMultiple
print("event.widget is {} and listbox is {} and listboxMultiple is {}\n".format(event.widget, listbox, listboxMultiple))
selection = event.widget.curselection()
listboxMultipleStr.set("")
if selection:
index = selection[0]
data = event.widget.get(index)
newvalue = "{}\n{}".format(data,"SOMETHING")
print("selected \"{}\"\n".format( data ))
print("newvalue is \"{}\"\n".format( newvalue ))
listboxMultiple.insert(END, "{}".format(data))
listboxMultiple.insert(END, "SOMETHING")
#listboxMultipleStr.set( newvalue )
else:
pass
def exitApplication():
global root
root.destroy()
if __name__ == "__main__":
main()
在 Windows 上使用 python3 7. 我为我的两个列表框小部件之一设置了回调“selectListItemCallback”。然而,当我单击“lb1”中的文本时,它按预期工作,我用相同的 selected 文本更新了“lb2”,并在“lb2”中添加了另一行。
问题是,当我 select“lb2”中的项目时,它仍然调用回调并且 event.widget 是“lb1”而不是“lb2”。
我的意图是在 'lb1' 中有一个项目列表,当我 select 其中任何一个时,'lb2' 就会充满与 select 相关的信息ed 'lb1' 项目。而且,我不想在我的 'lb2' 小部件中调用 selection 回调。
你能看出我做错了什么可能导致这种奇怪的行为吗?
谢谢。
我已经发布了代码;这在我的 Windows 7 机器上 运行 使用 python3。 Python 准确地说是 3.8.6。
这是因为当您单击另一个列表框时第一个列表框失去选择时会触发该事件。只要选择发生变化,事件就会触发,而不仅仅是在设置时触发。
如果您不希望在单击第二个列表框时第一个列表框丢失其选择,请将列表框的 exportselection
设置为 False
。否则,tkinter 将一次只允许一个选择。