为什么要为这个tkinter事件绑定调用多个函数?(已解决)



下面的代码片段创建了两个列表框。我写的代码是这样的:

  • 单击项目(通过OnASelect方法控制(后,从"可用"列表中选择的项目将转移到"选定"列表
  • 传输的项的索引保存在"可用"列表中(用空字符串替换(
  • 单击项目后,从"已选"列表中选择的项目将转移回"可用"列表
  • 由于"可用"列表的索引是保守的,因此无论单击项目的顺序如何,项目总是按原始顺序传输回

这是我的问题:

  • 尽管小部件按预期工作,但在将项目转移回"可用"列表时,我仍然会收到对OnASelect的不必要的函数调用,这会导致索引错误。这个小部件是为我正在进行的一个更大的项目准备的,我想避免这会导致后续的问题
  • 出于故障排除的目的,我编写了代码,以便在发生传输时,发送一条打印语句,显示"a>S功能已激活"或"S>功能已激活
  • 将项目从"可用"转移到"选定"时的输出:
The A>S function was activated
  • 将项目从"选定"转移到"可用"时的输出:
The S>A function was activated
The A>S function was activated
Exception in Tkinter callback
Traceback (most recent call last):
File "C:ProgramDataAnaconda3libtkinter__init__.py", line 1883, in __call__
return self.func(*args)
File "x:/Mouselight Data Management/GUI_Branch/GUI_Menu_Related/Test/test2.py", line 29, in OnASelect
AselectionIndex = int(event.widget.curselection()[0])
IndexError: tuple index out of range
PS X:Mouselight Data ManagementGUI_Branch> The A>S function was activate

我浪费了太多时间试图弄清楚它为什么这么做,我被难住了。如果能在这个问题上提供任何帮助,我们将不胜感激!

Ps。我知道在OnASelect方法的开头添加一个"if event.widget.curselection((:"可以绕过错误,但我想从一开始就阻止多函数调用。

完整代码:

from tkinter import *
class DependentLists(Tk):
def __init__(self): 
Tk.__init__(self)

self.mainframe = Frame(self)
self.mainframe.place(relx=0.5, rely=0.25, anchor=CENTER)
completelabel = Label(self.mainframe, text = "Available:")
completelabel.grid(row=2, column = 0)
selectedlabel = Label(self.mainframe, text = "Selected:")
selectedlabel.grid(row=2, column = 1)

self.Available = Listbox(self.mainframe)
self.Available.grid(row=3, column = 0)
self.AList = []
for i in range(6):
self.Available.insert(END,i)
self.AList.append(i)
self.Available.bind('<<ListboxSelect>>', self.OnASelect)
self.Selected = Listbox(self.mainframe)
self.Selected.grid(row=3, column = 1)
self.Selected.bind('<<ListboxSelect>>', self.OnSSelect)
def OnASelect(self, event):
print('The A>S function was activated')
AselectionIndex = int(event.widget.curselection()[0])
Aselection = event.widget.get(AselectionIndex)
self.Available.delete(AselectionIndex)
self.Available.insert(AselectionIndex,'')
self.Selected.insert(END, Aselection)
def OnSSelect(self, event):
print('The S>A function was activated')
SselectionIndex = int(event.widget.curselection()[0])
Sselection = event.widget.get(SselectionIndex)
self.Selected.delete(ANCHOR)
self.Available.delete(self.AList.index(Sselection))
self.Available.insert(self.AList.index(Sselection), Sselection)
App = DependentLists()
screen_width = App.winfo_screenwidth()
screen_height = App.winfo_screenheight()
window_height = screen_height - 800
window_width = screen_width - 1400
x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2) - (window_height/2))
App.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
App.mainloop()

编辑:解决方案在BryanOakley的回答中(谢谢!(以及该回答下的后续评论中。

只要选择发生更改,就会触发

<<ListboxSelect>>。这可能发生在用户单击某个项目时,但也可能发生在其他时间,例如用户使用键盘遍历列表框,或者您的代码删除了选定的内容。

如果您的UI应该只在单击鼠标时工作,那么您应该绑定到它,而不是<<ListboxSelect>>

最新更新