Python tkinter listbox get (ACTIVE) 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15672552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:40:35  来源:igfitidea点击:

Python tkinter listbox get (ACTIVE) method

pythonwidgettkinter

提问by Chris Aung

I was trying to make the currently selected List box item to be printed out. For example, when i select item "one", it should print out "one" and when i select item "two", it should print out "two" etc. The following is what i have tried.

我试图打印出当前选择的列表框项目。例如,当我选择项目“一”时,它应该打印出“一”,当我选择项目“二”时,它应该打印出“二”等。以下是我尝试过的。

from Tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx  = 40
posy  = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
itemsforlistbox=['one','two','three','four','five','six','seven']

def CurSelet(evt):
    value=str((mylistbox.get(ACTIVE)))
    print value

mylistbox=Listbox(root,width=60,height=10,font=('times',13))
mylistbox.bind('<<ListboxSelect>>',CurSelet)
mylistbox.place(x=32,y=90)

for items in itemsforlistbox:
    mylistbox.insert(END,items)
root.mainloop()

My problem is whenever i selected an item in the listbox, it is actually printing out the previously selected item.For example, the moment i select the item "two" in the list, it is printing out "one". To make things more clear,please see the following

我的问题是每当我在列表框中选择一个项目时,它实际上是在打印出之前选择的项目。例如,当我在列表中选择项目“二”时,它正在打印“一”。为了使事情更清楚,请参阅以下内容

(1) i selected the item "one", it printed out "one"

(1)我选择了项目“一”,它打印出“一”

(2) i selected the item "two", it print out "one" again

(2)我选择了项目“二”,它再次打印出“一”

(3) i selected the item "three", it print out "two" and so on...

(3)我选择了项目“三”,它打印出“二”等等......

Am i missing something? or did i misunderstand the way the get(ACTIVE) works? Appreciate for your help.

我错过了什么吗?还是我误解了 get(ACTIVE) 的工作方式?感谢您的帮助。

采纳答案by abarnert

An item becomes active after you click on it—which means after your ListboxSelectmethod returns. So, you're printing out whatever was active beforethis click (meaning, generally, what you clicked last time).

单击某个项目后,该项目将变为活动状态——这意味着在您的ListboxSelect方法返回之后。因此,您将打印出此次点击之前处于活动状态的任何内容(一般来说,就是您上次点击的内容)。

Also, given that you refer to "selected" numerous times, I think what you want is the selectedvalue(s), not the activeone, so you should be asking for that.

另外,鉴于您多次提到“选定”,我认为您想要的是选定的值,而不是活动值,因此您应该要求这样做。

For a listbox with selectmode=SINGLEor BROWSE(the default, what you have) listbox, you can fix both of these trivially. Just change this:

对于带有selectmode=SINGLEBROWSE(默认为您拥有的)列表框的列表框,您可以轻松修复这两个问题。只需改变这个:

mylistbox.get(ACTIVE)

to:

到:

mylistbox.get(mylistbox.curselection())

If you need to handle MULTIPLEor EXTENDED, then of course there are anywhere from 0 to 7 selections instead of exactly 1, so you need to do something like:

如果您需要处理MULTIPLEor EXTENDED,那么当然有 0 到 7 个选项而不是 1 个,因此您需要执行以下操作:

values = [mylistbox.get(idx) for idx in mylistbox.curselection()]
print ', '.join(values)

While we're at it, I'm not sure why you were doing str((mylistbox.get(ACTIVE))), or even str(mylistbox.get(ACTIVE)). The result of mylistbox.getwith a single index is going to be a string, the same one you inserted.

当我们这样做时,我不确定您为什么这样做str((mylistbox.get(ACTIVE))),甚至str(mylistbox.get(ACTIVE)). mylistbox.get使用单个索引的结果将是一个字符串,与您插入的字符串相同。

回答by Himel Das

This seems to work for me:

这似乎对我有用:

mylistbox.get(ANCHOR)

Based on your code, it will print out the current item.

根据您的代码,它将打印出当前项目。

回答by Mixstah

You could use this, it doesn't require the list box. So if you have multiple list boxes it will retrieve the value from any

您可以使用它,它不需要列表框。因此,如果您有多个列表框,它将从任何列表框检索值

from tkinter import*
root=Tk()
sizex = 600
sizey = 400
posx  = 40
posy  = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
itemsforlistbox=['one','two','three','four','five','six','seven']

def CurSelet(event):
    widget = event.widget
    selection=widget.curselection()
    picked = widget.get(selection[1])
    print(picked)

mylistbox=Listbox(root,width=60,height=10,font=('times',13))
mylistbox.bind('<<ListboxSelect>>',CurSelet)
mylistbox.place(x=32,y=90)

for items in itemsforlistbox:
    mylistbox.insert(END,items)
root.mainloop()

回答by Susan

The line picked = widget.get(selection[1])should be picked = widget.get(selection[0]).

该行picked = widget.get(selection[1])应该是picked = widget.get(selection[0])

Otherwise, there will be an "index out of range" error.

否则,将出现“索引超出范围”错误。

回答by F Karam

as stated by user1576772 the correct answer should be :

正如 user1576772 所说,正确答案应该是:

mylistbox.get(ANCHOR)

The Problem with curselection()is it returns the whole list currently selected each time.

问题curselection()在于它每次都返回当前选择的整个列表。

consider a situation where you scroll through a Listbox and want always to get the last selected item forward or backward (not the last item in highlighted list , the last item pressed on by user )! curselection()will give you all the currently selected items which is helpful but you will never know which item was selected last !

考虑一种情况,您滚动列表框并希望始终向前或向后获取最后一个选定的项目(不是突出显示列表中的最后一个项目,用户按下的最后一个项目)!curselection()将为您提供所有当前选择的项目,这很有帮助,但您永远不会知道最后选择了哪个项目!