Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ttk_optionmenu_bug_fix_demo.py
- import tkinter as tk
- from tkinter import StringVar
- from tkinter import ttk
- import string
- sss = string.ascii_uppercase
- root = tk.Tk()
- root.title("ttk.OptionMenu Demo")
- def clear_and_disable():
- for i, option_menu, _ in option_menus:
- var[i].set("")
- option_menu['menu'].delete(0, 'end')
- option_menu.state(["disabled"])
- def populate_and_enable():
- for i, option_menu, options in option_menus:
- for option in options:
- option_menu['menu'].add_command(label=option, command=lambda opt=option, i=i: var[i].set(opt))
- option_menu.state(["!disabled"])
- var[i].set(options[0])
- var = {}
- option_menus = []
- for i in range(0, 5):
- var[i] = StringVar(root)
- options = [f"Option {sss[i+(j*5)]}" for j in range(5)]
- option_menu = ttk.OptionMenu(root, var[i], *[options[0]] + options) # *[options[0]] + options fixes an OptionMenu bug
- option_menu.pack(pady=3)
- option_menus.append((i, option_menu, options))
- var[i].set(options[0])
- clear_button = tk.Button(root, text="Clear and Disable", command=clear_and_disable)
- clear_button.pack(fill='x')
- enable_button = tk.Button(root, text="Populate and Enable", command=populate_and_enable)
- enable_button.pack(fill='x')
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement