tkinter 列表框 get(ACTIVE) 方法 [英] tkinter listbox get(ACTIVE) method

查看:44
本文介绍了tkinter 列表框 get(ACTIVE) 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打印出当前选定的 Listbox 项目.例如,当我选择项目一个"时,它应该打印出一个";当我选择项目二"时,它应该打印出二"等等.以下是我尝试过的.

I was trying to make the currently selected Listbox 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. 我选择了项目一个",它打印出一个"
  2. 我选择了项目二",它打印出一";再次
  3. 我选择了项目三",它打印出二";等等...

我错过了什么吗?还是我误解了 get(ACTIVE) 的工作方式?

Am I missing something? or did I misunderstand the way the get(ACTIVE) works?

推荐答案

一个项目在您点击后变为活动状态——这意味着在您的 ListboxSelect 方法返回之后.因此,您将打印出此次点击之前 处于活动状态的任何内容(通常意味着您上次点击的内容).

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

另外,鉴于您多次提到selected",我认为您想要的是 selected 值,而不是 active 值,所以您应该问那个.

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

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

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

mylistbox.get(ACTIVE)

到:

mylistbox.get(mylistbox.curselection())

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

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

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

虽然我们在做,但我不确定你为什么要这样做 str((mylistbox.get(ACTIVE))),甚至 str(mylistbox.get(ACTIVE))).带有单个索引的 mylistbox.get 的结果将是一个字符串,与您插入的相同.

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.get with a single index is going to be a string, the same one you inserted.

这篇关于tkinter 列表框 get(ACTIVE) 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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