python - 使用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
将事件处理程序绑定到组合框的多个示例。几乎所有都只是从事件处理程序中打印选定的值。其他方法将字符串变量跟踪到事件处理程序。
我不知道如何将返回值放入主体。我的目标是使用它成为标签的文本,同时也是字典中的索引或键。
建议表示赞赏。
解决方案
推荐阅读
- esp32 - Esp32 低频 PWM
- java - 如何使用java流过滤地图的地图
- javascript - 单击复选框时,数组会丢失其值
- android - Firebase Firestore Android 如何创建带有布尔字段 onClick 的文档
- android - 如果使用 android:process 启动一个新进程,这个进程会再次加载所有的 dex 吗?
- java - Java中ArrayList内部的HashMap被覆盖
- javascript - 将 Google 服务帐户与 google API JavaScript 客户端一起使用
- excel - 如何在第二行自动过滤
- visual-studio-code - 设置文件编码
- javascript - ReactJS & TypeScript - 获取数组的一个随机值