单击按钮后重新启动 Combobox tkinter

Restart the Combobox tkinter after clicking a button

我是 Python 和 Tkinter 的新手。我遵循 Python + MySQL. How can I pass the ID of a field from a ComboBox to the database? 作为通过索引获取值的指南。当我点击 Submit 按钮时,Combobox 仍然显示之前的值。

我的代码:

import tkinter as tk
from tkinter import ttk

def data_set(index):
    index = cb.current()
    data.set(c[index][0])
    
c = [
  (123, 'A'),
  (124, 'B'),
  (125, 'C'),
]

def clear():
  print(data.get())
  # I need the Combobox shows an empty value before clicking the dropdown button.
  
root = tk.Tk()
data = tk.StringVar()

cb = ttk.Combobox(root,values=[row[1] for row in c], state='readonly')
cb.pack()
cb.bind("<<ComboboxSelected>>", data_set)

btn = tk.Button(root, text="Submit",command=clear)
btn.pack()

root.mainloop()

选择A并点击Submit按钮时的输出:

123

但是 Combobox 仍然显示以前的值:

我正在寻找一种方法,比如在选择 A 并单击 Submit 按钮后,123 的输出打印到终端,然后 Combobox 去回到开头如下:

请帮助我。非常感谢。

如果您要在 Combobox 中使用 StringVartextvariable,那么您可以在 StringVar 中设置空字符串以重置 Combobox

import tkinter as tk
from tkinter import ttk

c = [
  (123, 'A'),
  (124, 'B'),
  (125, 'C'),
]

def clear():
    index = cb.current()
    print(data.get())    # A
    print(c[index][0])   # 123
    data.set('')         # reset

root = tk.Tk()
data = tk.StringVar(root)

cb = ttk.Combobox(root, textvariable=data, values=[row[1] for row in c], state='readonly')
cb.pack()

btn = tk.Button(root, text="Submit", command=clear)
btn.pack()

root.mainloop()