0

I created this tkinter dropdown window where I select an option. After I choose that I press Ok and the option gets printed. How can I close this tkinter window after pressing the 'Ok' button?

from tkinter import *

OPTIONS = [
"Physician 1",
"Physician 2",
"Physician 3"
]

master = Tk()

variable = StringVar(master)
variable.set(OPTIONS[0]) # default value

w = OptionMenu(master, variable, *OPTIONS)
w.pack()

def ok():
    physician_name=variable.get()
    print (physician_name)

button = Button(master, text="OK", command=ok)
button.pack()

mainloop()
Cristian
  • 93
  • 1
  • 7
  • Does this answer your question? [How do I close a tkinter window?](https://stackoverflow.com/questions/110923/how-do-i-close-a-tkinter-window) – PCM Jul 17 '21 at 02:55

2 Answers2

2

I think you have to include master.destroy() after printing:

from tkinter import *

OPTIONS = [
"Physician 1",
"Physician 2",
"Physician 3"
]

master = Tk()

variable = StringVar(master)
variable.set(OPTIONS[0]) # default value

w = OptionMenu(master, variable, *OPTIONS)
w.pack()

def ok():
    physician_name=variable.get()
    print (physician_name)
    master.destroy()

button = Button(master, text="OK", command=ok)
button.pack()

mainloop()
DeusDev
  • 538
  • 6
  • 15
1

Do you mean close the entire application? Call master.destroy() within the ok function.