Python Forum

Full Version: changing the frame colour
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to change the background of a frame using four colour buttons placed at four corners of the frame.
I was trying by adding only one button in the frame and changing the color when button is pressed but its not changing the color when the button is in the frame and works when the button is in the window.
import tkinter as tk

root = tk.Tk()
root.geometry("100x100")
frame = tk.Frame(root, height=100, width=100)


def change_bg():
    frame.config(background='red')
    

button = tk.Button(frame, text="Paint",command=change_bg)


button.pack()
frame.pack()
root.mainloop()
Hi nick123

Now it should work:
import tkinter as tk
 
root = tk.Tk()
root.geometry("100x100")
frame = tk.Frame(root, height=100, width=100)
 
 
def change_bg():
    frame.config(background='red')
     
 
button = tk.Button(frame, text="Paint",command=change_bg)
 
 
button.pack()
frame.pack(fill='both', expand=True)
root.mainloop()
wuf :-)
It worked ! Thank You wuf!
(Apr-25-2019, 02:13 PM)nick123 Wrote: [ -> ]I want to change the background of a frame using four colour buttons placed at four corners of the frame.
I was trying by adding only one button in the frame and changing the color when button is pressed but its not changing the color when the button is in the frame and works when the button is in the window.
import tkinter as tk

root = tk.Tk()
root.geometry("100x100")
frame = tk.Frame(root, height=100, width=100)


def change_bg():
    frame.config(background='red')
    

button = tk.Button(frame, text="Paint",command=change_bg)


button.pack()
frame.pack()
root.mainloop()

I tried this and I get the error:
Error:
File "/storage/emulated/0/Python/RGBsliders.py", line 19, in update def update(rgb): AttributeError: 'NoneType' object has no attribute 'config'
Heres my code:
import tkinter as tk

def rgb_to_hex(rgb):
    return '#%02x%02x%02x' % rgb

root = tk.Tk()

r = tk.StringVar(root)
g = tk.StringVar(root)
b = tk.StringVar(root)

r.set(0)
g.set(0)
b.set(0)

def update(rgb):
	global preview
	hex = rgb_to_hex(rgb)
	preview.config(background=hex)

def set_r(val):
    r.set(val)
    update((int(r.get()), int(g.get()), int(b.get())))

def set_g(val):
    g.set(val)
    
def set_b(val):
    b.set(val)

preview = tk.Frame(root, height=200, width=200, bg=rgb_to_hex((0, 0, 0))).place(relx=0.5, rely=0.2, anchor=tk.CENTER)

tk.Label(root, textvariable=r).place(relx=0.25, rely=0.8, anchor=tk.CENTER)

tk.Scale(root, command=set_r,  showvalue=0, to=255, length=500, width=50).place(relx=0.25, rely=0.5, anchor=tk.CENTER)

tk.Label(root, textvariable=g).place(relx=0.5, rely=0.8, anchor=tk.CENTER)

tk.Scale(root, command=set_g,  showvalue=0, to=255, length=500, width=50).place(relx=0.5, rely=0.5, anchor=tk.CENTER)

tk.Label(root, textvariable=b).place(relx=0.75, rely=0.8, anchor=tk.CENTER)

tk.Scale(root, command=set_b,  showvalue=0, to=255, length=500, width=50).place(relx=0.75, rely=0.5, anchor=tk.CENTER)

root.mainloop()
I fixed it, turns out that you cannot do
var = Label(root).place()
There goes my cheat method out of the window!