Reputation: 7
How to take user-selected colour and use it as the background colour of the tkinter frame?
list2 = ["red", "red", "red", "red", "blue", "yellow"];
droplist = OptionMenu(root, c, *list2)
droplist.config(width=15)
c.set('select your colour')
droplist.place(x=240, y=320)
root.configure(bg=c)
Upvotes: 0
Views: 127
Reputation: 41862
Let's make this work by filling in some missing pieces:
import tkinter as tk
COLORS = ["red", "blue", 'green', 'cyan', 'magenta', "yellow"]
def change_color(*args):
root.configure(bg=color.get())
root = tk.Tk()
root.minsize(width=200, height=200)
color = tk.StringVar(root)
color.trace('w', change_color)
color.set(COLORS[0])
om = tk.OptionMenu(root, color, *COLORS)
label = tk.Label(root, text='Select your color')
om.pack(side="top")
label.pack(side="top")
root.mainloop()
The primary missing piece was the StringVar
associated with the OptionMenu
which allows you to interogate it. To associate a callback function with the OptionMenu
, we trace changes to the StringVar
.
Upvotes: 3