Quitting Tkinter drop down menu
The problem with calling root.quit()
in function()
was caused by the fact that it's a variable local to the edit_or_retrieve()
function. This can be fixed by passing it as an argument to function()
, but unfortunately, the OptionMenu
widget is the thing that does this, and you can't modify it.
However you can workaround this and pass extra arguments to the function by creating a short lambda
function that acts as a software "shim" and passes the extra argument to function()
when it's called.
Below is your code with the modifications to do this:
from tkinter import *def edit_or_retrieve(): root = Tk() root.title("Main Menu") menu = Frame(root) menu.pack(pady=5, padx=50) var = StringVar(root) options = ['Enter', 'Edit', 'Retrieve', 'Report 1 - Dates of birth', 'Report 2 - Home phone numbers', 'Report 3 - Home addresses', 'Log off',] option = OptionMenu(menu, var, *options, # use lambda to pass local var as extra argument command=lambda x: function(x, root)) var.set('Select') option.grid(row=1, column=1) root.mainloop()def function(value, root): # note added "root" argument if value == 'Edit': edit() if value == 'Enter': enter() if value == 'Retrieve': display() if value == 'Report 1 - Dates of birth': reportone() if value == 'Report 2 - Home phone numbers': reporttwo() if value == 'Report 3 - Home addresses': reportthree() if value == 'Log off': root.quit()edit_or_retrieve()