在列表框中获取所选项目,然后调用另一个为其存储所选内容的函数 [英] Get selected item in listbox and call another function storing the selected for it

查看:99
本文介绍了在列表框中获取所选项目,然后调用另一个为其存储所选内容的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个画布,单击它会调用createCategoryMeny(x).

I have a canvas that calls createCategoryMeny(x) when it is clicked.

此功能仅创建一个Toplevel()窗口,

This function just creates a Toplevel() window,

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 !!".

This successfully prints `"ThisShouldBeTheSelected!!"``.

这就是我完全被困住的地方.

And this is where I am utterly stuck.

我无法获得双击的元素(选定的元素).

I cant get the double clicked element (the selected one).

我想将其作为tag=传递.

我尝试过:

listbox.curselection()

始终打印('0',)

如果我删除listbox.selection_set(first=0),则只会得到此内容:()

If I remove listbox.selection_set(first=0), I only get this: ()

所以问题是:

  • 如何获取所选项目(双击的项目)
  • (不是很重要)像我一样将其传递给其他函数是否合理?

注意:

我发现了:

8.5. .listbox为什么选择或选择不返回 当我将按钮绑定到列表框时该选择正确的项目吗?

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:

绑定.listbox {设置项目[%W得到[%W最接近%y]]}

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

这可确保指针下的项目是将要返回的内容 作为项目. .listbox curselection失败的原因是 在Listbox类绑定之前,不会设置curselection中的项目 触发器,默认情况下在实例绑定之后.这是 获得选择的相同原因可能会失败,但同时也会 如果将-exportselection选项设置为0,将失败.

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.

我不确定这是否有帮助,我不太了解它在说什么.

I'm not sure if it's helpful, I don't really understand what it is saying.

推荐答案

请不要使用lambda.它对范围狭窄的问题很有用,这不是问题之一.创建适当的功能,它们更容易编写和维护.

For one, don't use lambda. It's useful for a narrow range of problems and this isn't one of them. Create a proper function, they are much easier to write and maintain.

完成此操作后,可以调用curselection以获得当前选择.您说您尝试过,但是示例代码没有显示您尝试过的内容,因此我只能假设您做错了.

Once you do that, you can call curselection to get the current selection. You say you tried that but your example code doesn't show what you tried, so I can only assume you did it wrong.

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

As for the rather unusual advice to use nearest... all it's saying is that bindings you put on a widget happen before default bindings for that same event. It is the default bindings that set the selection, so when you bind to a single button click, your binding fires before the selection is updated by the default bindings. There are many ways around that, the best of which is to not bind on a single click, but instead bind on <<ListboxSelect>> which will fire after the selection has changed.

但是,您没有这个问题.由于您是通过双击进行绑定的,因此选择将通过默认的单击绑定进行设置,并且curselection将返回正确的值.也就是说,除非您有自己的单击绑定,否则无法触发默认绑定.

You don't have that problem, however. Since you are binding on a double-click, the selection will have been set by the default single-click binding and curselection will return the proper value. That is, unless you have your own single-click bindings that prevent the default binding from firing.

这是一个简单的示例,它打印出选择内容,因此您可以看到它是正确的.从命令行运行它,这样您将看到stdout:

Here's a simple example that prints out the selection so you can see it is correct. Run it from the command line so you see stdout:

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()

这篇关于在列表框中获取所选项目,然后调用另一个为其存储所选内容的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆