tkinter 列表框 get(ACTIVE) 方法

编程入门 行业动态 更新时间:2024-10-28 12:24:19
本文介绍了tkinter 列表框 get(ACTIVE) 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我试图打印出当前选定的 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

我选择了项目一个",它打印出一个"我选择了项目二",它打印出一";再次我选择了项目三",它打印出二";等等...

我错过了什么吗?还是我误解了 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 08:52:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1392481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   列表   tkinter   ACTIVE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!