您可以使用bind()
执行函数on_select
当您选择列表中的元素时。
cb.bind('<<ComboboxSelected>>', on_select)
在这个函数中你可以填写Entry
.
来自 GitHub 的旧示例:组合框获取选择 https://github.com/furas/python-examples/blob/master/tkinter/combobox-get-selection/main.py
#!/usr/bin/env python3
import tkinter as tk
import tkinter.ttk as ttk
# --- functions ---
def on_select(event=None):
print('----------------------------')
if event: # <-- this works only with bind because `command=` doesn't send event
print("event.widget:", event.widget.get())
for i, x in enumerate(all_comboboxes):
print("all_comboboxes[%d]: %s" % (i, x.get()))
# --- main ---
root = tk.Tk()
all_comboboxes = []
cb = ttk.Combobox(root, values=("1", "2", "3", "4", "5"))
cb.set("1")
cb.pack()
cb.bind('<<ComboboxSelected>>', on_select)
all_comboboxes.append(cb)
cb = ttk.Combobox(root, values=("A", "B", "C", "D", "E"))
cb.set("A")
cb.pack()
cb.bind('<<ComboboxSelected>>', on_select)
all_comboboxes.append(cb)
b = tk.Button(root, text="Show all selections", command=on_select)
b.pack()
root.mainloop()
EDIT:
Line if event:
in on_select
仅当您使用时才有效bind()
因为它执行带有事件信息的函数。command=
执行不带参数的函数,然后设置even=None
进而if event:
总是False
.