如何用记录填充组合框并打印选定的相应记录

时间:2018-03-28 10:58:34

标签: python tkinter combobox

尝试使用我的元组中的spaecific记录填充tkinter combobox 然后,当我选择组合框中的记录打印我想得到的 相应的记录未在组合框中填充。

它填充了combobox 1 ben journalism 18之类的内容。但是我希望像ben那样将其打包出来,然后当我选择它时会像1 ben journalism 18那样打印。

from tkinter import *
from tkinter import ttk



rows = ((1, 'ben', 'journalism', 18), (2, 'sally', 'performing arts', 22),(3,"dan","information technology",32))


def selected(event=None):
    print(cb.get()) # print selected record in other column


root = Tk()

cb = ttk.Combobox(root)
cache = list()
for row in rows:
    cache.append(row)
    cb['values'] = cache

cb.pack()
cb.bind("<<ComboboxSelected>>", selected)

root.mainloop()

1 个答案:

答案 0 :(得分:1)

一些小的改动应该为你解决。首先,我假设行是列表,如[[1,'ben','新闻',18],[2,'sally','表演艺术',22] ...]

def selected(event=None):
    # Find the row that contains the selected student's name
    for row in rows:
        if cb.get() in row:
            # Found it. Print it.
            print(row)
            break

for row in rows:
    # Insert just the student name into combobox
    cache.append(row[1])
cb['values'] = cache