Python Forum
[Tkinter] changing the frame colour
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] changing the frame colour
#1
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()
Reply
#2
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 :-)
Reply
#3
It worked ! Thank You wuf!
Reply
#4
(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()
Reply
#5
I fixed it, turns out that you cannot do
var = Label(root).place()
There goes my cheat method out of the window!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't change the colour of Tk button text Pilover 6 14,491 Nov-15-2022, 10:11 PM
Last Post: woooee
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 4,005 Sep-30-2021, 05:57 PM
Last Post: menator01
  Making a colour field Leo_Red 11 4,827 Jan-22-2021, 04:15 PM
Last Post: BashBedlam
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 5,626 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  Why is wx.NO_BORDER changing panels within a frame MeghansUncle2 4 2,506 Jul-12-2020, 12:32 PM
Last Post: Yoriz
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 8,937 Sep-30-2019, 07:30 AM
Last Post: Maksim
  [Tkinter] create and insert a new frame on top of another frame atlass218 4 10,991 Apr-18-2019, 05:36 PM
Last Post: atlass218
  [Tkinter] Frame size only works if frame is empty(Solved) Tuck12173 7 6,367 Jan-29-2018, 10:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020