如何获得组合框的选定值(并将其存储在变量中)?

时间:2018-07-27 10:48:00

标签: python tkinter

我一直想知道如何获取组合框的当前值(而不是current()),并且最好将其存储到变量“ listvalue”中。我已经搜索了一段时间,但从未找到想要的东西。 这是示例代码:

FontList1['values'] = ("Courier","Courier New","Times New Roman","Comic Sans MS")
FontList1.set(TypeFace)
print(listvalue)
FontList1.pack(side="right")

1 个答案:

答案 0 :(得分:0)

为什么不将所选字体存储在tkinter StringVar中?

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

selectedFontVar = tk.StringVar()

FontList1 = ttk.Combobox(root,textvariable=selectedFontVar,postcommand=displayFont)
FontList1['values'] = ("Courier","Courier New","Times New Roman","Comic Sans MS")
FontList1.pack()

SelectedFont = tk.Entry(root,textvariable=selectedFontVar)
SelectedFont.pack()

root.mainloop()

如果要获取选定的字体,只需调用“ selectedFontVar.get()”,它将返回组合框中选择的所有内容。

StringVar的优点是,在组合框更改时会自动更新,并且此更改可以“层叠”到也使用相同stringvar的其他小部件。就像我的示例中的SelectedFont。

相关问题