首页 > 解决方案 > 使用TTK Combobox,如何在程序的其他地方使用返回值?

问题描述

`enter code here`
from tkinter import *
from tkinter.ttk import *

def on_field_change(index, value, op):
    choice=StringVar  
    print( "combobox updated to ", c.get() ) # does print the updated values
    #choice=c.get
    #return(choice)  # added a return for testing - not useful
chosen=StringVar 
chosen=""   
root = Tk()
root.geometry("400x200+10+10")
v = StringVar()
x = StringVar
chosen=StringVar 
chosen=""   
c = Combobox(root, textvar=v, values=["foo", "bar", "baz"])
c.set('foog')
c.grid(row=0, column=1)

#choice_label = ttk.Label(text='choice='+c.get() ) #does not update
choice_label = Label(text= c.get ) #does not update
choice_label = Label(text= v ) #does not update
choice_label.grid(row=3, column=0)

quit_button = ttk.Button(root, text="QUIT")
quit_button.grid(row=4, column=0)
quit_button['command'] = root.destroy
v.trace('w',on_field_change)
print('the new v is : ', c.get())  # does not print updated values
#x=v.trace('w',on_field_change)
#print('the new choice is : ', x ) # does not print updated values
# try to use the selected item in another label and for a dictionary search
mainloop()

enter code here

将事件处理程序绑定到组合框的多个示例。几乎所有都只是从事件处理程序中打印选定的值。其他方法将字符串变量跟踪到事件处理程序。

我不知道如何将返回值放入主体。我的目标是使用它成为标签的文本,同时也是字典中的索引或键。

建议表示赞赏。

标签: pythontkinterttk

解决方案


推荐阅读