Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_OptionMenu_swap.py
- from Tkinter import *
- def reset_option_menu(options, index=None):
- '''reset the values in the option menu
- if index is given, set the value of the menu to
- the option at the given index
- '''
- menu = om["menu"]
- menu.delete(0, "end")
- for string in options:
- menu.add_command(label=string,
- command=lambda value=string:
- om_variable.set(value))
- if index is not None:
- om_variable.set(options[index])
- #
- def use_colors():
- '''Switch the option menu to display colors'''
- reset_option_menu(["Red","Orange","Green","Blue"], 0)
- #
- def use_sizes():
- '''Switch the option menu to display sizes'''
- reset_option_menu(["X-Small", "Small", "Medium", "Large"], 0)
- #
- root = Tk()
- om_variable = StringVar()
- b1 = Button(text="Colors", width=8, command=use_colors)
- b2 = Button(text="Sizes", width=8, command=use_sizes)
- om = OptionMenu(root, om_variable, ())
- om.configure(width=20)
- use_colors()
- b1.pack(side="left")
- b2.pack(side="left")
- om.pack(side="left", fill="x", expand=True)
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement