Advertisement
here2share

# tk_optionmenu.py

Dec 24th, 2020
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. # tk_optionmenu.py
  2.  
  3. from Tkinter import *
  4.  
  5. root = Tk()
  6. root.title("Tk OptionMenu")
  7.  
  8. # Add a grid
  9. mainframe = Frame(root)
  10. mainframe.grid(column=0,row=0, sticky=(N,W,E,S))
  11. mainframe.columnconfigure(0, weight = 1)
  12. mainframe.rowconfigure(0, weight = 1)
  13. mainframe.pack(pady = 50, padx = 50)
  14.  
  15. # Create a Tkinter variable
  16. tkvar = StringVar(root)
  17.  
  18. # Dictionary with options
  19. choices = { 'Pizza','Lasagne','Fries','Salad','Cheeseburger'}
  20. tkvar.set('Fries') # set the default option
  21.  
  22. popupMenu = OptionMenu(mainframe, tkvar, *choices)
  23. Label(mainframe, text="Choose A Dish").grid(row = 1, column = 1)
  24. popupMenu.grid(row = 2, column =1)
  25.  
  26. # on change dropdown value
  27. def change_dropdown(*args):
  28.     print( tkvar.get() )
  29.  
  30. # link function to change dropdown
  31. tkvar.trace('w', change_dropdown)
  32.  
  33. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement