Python 如何获取组合框的选定值(并将其存储在变量中)?
我一直想知道如何获取组合框的当前值(不是current()),并最好将其存储到我的变量“listvalue”中。我已经找了一段时间了,但没有找到我想要的东西。 以下是示例代码:Python 如何获取组合框的选定值(并将其存储在变量中)?,python,tkinter,Python,Tkinter,我一直想知道如何获取组合框的当前值(不是current()),并最好将其存储到我的变量“listvalue”中。我已经找了一段时间了,但没有找到我想要的东西。 以下是示例代码: FontList1['values'] = ("Courier","Courier New","Times New Roman","Comic Sans MS") FontList1.set(TypeFace) print(listvalue) FontList1.pack(side="right") 为什么不将所选字
FontList1['values'] = ("Courier","Courier New","Times New Roman","Comic Sans MS")
FontList1.set(TypeFace)
print(listvalue)
FontList1.pack(side="right")
为什么不将所选字体存储在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