gpt4 book ai didi

python - Tkinter - 旋转框中选定值的索引是多少?

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:51 24 4
gpt4 key购买 nike

我使用 Tkinter Spinbox 构建了一个用于项目选择的类。我想返回所选项目的索引。这可能吗?

from Tkinter import *

class TestSpin():
def __init__(self, i_root, i_friendlist):
self.root = i_root
self.root.friend = Spinbox(self.root, values = i_friendlist)
self.root.friend.pack()
self.root.okbutton = Button(self.root, text='Ok', command = lambda: self.ack())
self.root.okbutton.pack()

def ack(self):

# here I'd like to have the index of selected item in friendlist
print "Index of "+self.root.friend.get()+" is [0 or 1 or 2]:", self.root.friend
# here I'd like to have the index of selected item in friendlist

self.root.destroy()

root = Tk()
list = ['Emma','Sarah','James']
app = TestSpin(root, list)
root.mainloop()

非常感谢您的建议。我尝试了 self.root.friend.index(),但该方法需要一个我不理解的参数。

最佳答案

没有办法直接从小部件中获取值,但这只是一行Python代码:

index = i_fiendlist.index(self.root.friend.get())

使用您的代码,您需要保存列表的值:

class TestSpin():
def __init__(self, i_root,i_friendlist):
self._list = i_list
...

def ack(self):
value = self.root.friend.get()
index = self._list.index(value)
print "Index of " + value + " is " + str(index)
self.root.destroy()

关于python - Tkinter - 旋转框中选定值的索引是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35850767/

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