Home > database >  Restart the Combobox tkinter after clicking a button
Restart the Combobox tkinter after clicking a button

Time:03-26

I'm new to Python and Tkinter. I'm following Still display the previous value

I'm looking for a way like after choosing A and clicking the Submit button, the output of 123 print to terminal, then the Combobox go back to the beginning like below:

The combobox go back to the beginning

Please help me. Thank you so much.

CodePudding user response:

if you would use StringVar with textvariable in Combobox then you could set empty string in StringVar to reset 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()
  • Related