Listbox listvariable.get()返回元组,但却被当作字符串处理?

问题描述 投票:0回答:1

在一个类中工作,我试图获取列表框中的选择项,但是当我应用选择指数时,似乎元组已经变成了字符串。

def __init__(self, parent):
    # <---snip--->
    self.part_no = tk.StringVar()
    self.part_listbox = tk.Listbox(self, listvariable=self.part_no, selectmode='multiple')
    # <---snip--->

def load_part(self):

    part_list = self.part_no.get() # tuple of strings
    selection = self.part_listbox.curselection() # tuple of integers

    load_list = [part_list[item] for item in selection] # list of characters???
    print(part_list, selection, load_list)

选择前三个值(并按下load_part命令的按钮),我得到了输出。

('101', '201', '301', '401', '501') (0, 1, 2) ['(', "'", '1']

我希望如此

('101', '201', '301', '401', '501') (0, 1, 2) ['101', '201', '301']

谁能告诉我这是怎么回事?

Python 3.8.2,Tkinter 8.6。

python tkinter listbox tuples
1个回答
1
投票

答案:"是的。因为前提是假的。

listvariable.get()返回的是一个字符串,而不是元组。

经验教训。一定要仔细检查对象类型。

© www.soinside.com 2019 - 2024. All rights reserved.