gpt4 book ai didi

python - 在列表框中获取所选项目并调用另一个函数存储所选项目

转载 作者:太空狗 更新时间:2023-10-30 00:27:00 24 4
gpt4 key购买 nike

我有一个 Canvas ,当它被点击时调用 createCategoryMeny(x)

这个函数只是创建一个 Toplevel() 窗口,

def createCategoryMenu(tableNumber):

##Not interesting below:
categoryMenu = Toplevel()
categoryMenu.title("Mesa numero: " + str(tableNumber))
categoryMenu.geometry("400x400+100+100")

categoryMenu.focus()
categoryMenu.grab_set()

Label(categoryMenu, text="Elegir categoria \n Mesa numero: " + str(tableNumber)).pack()


## RELEVANT CODE BELOW:
listbox = Listbox(categoryMenu, width=50, height=len(info.listaCategorias))
listbox.pack(pady=20)

for item in info.listaCategorias:
listbox.insert(END, item)

listbox.selection_set(first=0)

##My attempt to solve it
callback = lambda event, tag= "ThisShouldBeTheSelected!!": do(event, tag)
listbox.bind("<Double-Button-1>", callback)

然后是do函数:

def do(event, tag):
print tag

这成功打印了`"ThisShouldBeTheSelected!!"``。

这就是我完全陷入困境的地方。

我无法获取双击的元素(选中的元素)。

我想将其作为 tag= 传递。

我试过:

listbox.curselection()

总是打印 ('0',)

如果我删除 listbox.selection_set(first=0),我只会得到这个:()

所以问题是:

  • 如何获取选中的项(双击项)
  • (不那么重要)像我一样把它传递给其他函数是否合理?

注意:

我找到了 this :

8.5. Why doesn't .listbox curselection or selection get return the proper item when I make a button binding to my listbox?

The best way to get the selected item during a button click event on a listbox is to use the following code:

bind .listbox { set item [%W get [%W nearest %y]] }

This ensures that the item under the pointer is what will be returned as item. The reason .listbox curselection can fail is because the items in curselection are not set until the Listbox class binding triggers, which is after the instance bindings by defaults. This is the same reason for which selection get can fail, but it will also fail if you set the -exportselection option to 0.

我不确定它是否有帮助,我不太明白它在说什么。

最佳答案

首先,不要使用 lambda .它对范围狭窄的问题很有用,这不是其中之一。创建一个合适的函数,它们更容易编写和维护。

完成后,您可以调用 curselection获取当前选择。你说你试过了,但你的示例代码没有显示你试过的东西,所以我只能假设你做错了。

至于使用 nearest 的相当不寻常的建议......它只是说你放在小部件上的绑定(bind)发生在同一事件的默认绑定(bind)之前。设置选择的是默认绑定(bind),因此当您绑定(bind)到单击单个按钮时,您的绑定(bind)会在默认绑定(bind)更新选择之前触发。有很多解决方法,其中最好的方法是单击绑定(bind),而是绑定(bind)到 <<ListboxSelect>>。选择更改后将触发。

但是,您没有这个问题。由于您是通过双击进行绑定(bind),因此选择将由默认的单击绑定(bind)和 curselection 设置。将返回正确的值。也就是说,除非您有自己的单击绑定(bind)来阻止触发默认绑定(bind)。

这是一个打印出选择的简单示例,因此您可以看到它是正确的。从命令行运行它,以便您看到标准输出:

import Tkinter as tk

class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
lb = tk.Listbox(self)
lb.insert("end", "one")
lb.insert("end", "two")
lb.insert("end", "three")
lb.bind("<Double-Button-1>", self.OnDouble)
lb.pack(side="top", fill="both", expand=True)

def OnDouble(self, event):
widget = event.widget
selection=widget.curselection()
value = widget.get(selection[0])
print "selection:", selection, ": '%s'" % value

if __name__ == "__main__":
app = SampleApp()
app.mainloop()

关于python - 在列表框中获取所选项目并调用另一个函数存储所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7616541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com