Advertisement
here2share

# Tk_OptionMenu_swap.py

Jun 2nd, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. # Tk_OptionMenu_swap.py
  2.  
  3. from Tkinter import *
  4.  
  5. def reset_option_menu(options, index=None):
  6.     '''reset the values in the option menu
  7.  
  8.     if index is given, set the value of the menu to
  9.     the option at the given index
  10.     '''
  11.     menu = om["menu"]
  12.     menu.delete(0, "end")
  13.     for string in options:
  14.         menu.add_command(label=string,
  15.                          command=lambda value=string:
  16.                               om_variable.set(value))
  17.     if index is not None:
  18.         om_variable.set(options[index])
  19. #
  20. def use_colors():
  21.     '''Switch the option menu to display colors'''
  22.     reset_option_menu(["Red","Orange","Green","Blue"], 0)
  23. #
  24. def use_sizes():
  25.     '''Switch the option menu to display sizes'''
  26.     reset_option_menu(["X-Small", "Small", "Medium", "Large"], 0)
  27. #
  28.  
  29. root = Tk()
  30.  
  31. om_variable = StringVar()
  32.  
  33. b1 = Button(text="Colors", width=8, command=use_colors)
  34. b2 = Button(text="Sizes", width=8, command=use_sizes)
  35.  
  36. om = OptionMenu(root, om_variable, ())
  37. om.configure(width=20)
  38. use_colors()
  39.  
  40. b1.pack(side="left")
  41. b2.pack(side="left")
  42. om.pack(side="left", fill="x", expand=True)
  43.  
  44.  
  45. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement