as you can read in the title, i'm trying to set the background of a frame to a random colour everytime the button is pressed. I set up a variable which holds the colours. I tested that and it does print everytime a different random colour. But when i assaign it to the Button, nothing changes..
import random
from tkinter import *
def overlord():
counter = 0
main_frame = Tk()
main_frame.title("I'm bored pls Help")
main_frame.attributes("-fullscreen", True)
# main_frame.config(background="black")
counter_l = Label(text=counter)
random_colour_b = Button(main_frame, bg="black", fg="gold", text="Click Me!", font="Arial 20",
command=lambda: random_colour_b)
exit_b = Button(text="Kill Me!", bg="black", fg="red", font="Arial 20", command=main_frame.destroy)
counter_l.pack()
random_colour_b.pack()
exit_b.pack()
main_frame.mainloop()
Thats my function for the Frame. I can't seem to find an answer which tells me how to change the frame background with the Button. Anyone got an idea?
CodePudding user response:
I am not competent in Tkinter, but there is a solution (even if terms is wrong):
command
parameter accepts a function that performs an action on your frame. You used it in incorrect way
from random import choice
from tkinter import *
def overlord():
counter = 0
main_frame = Tk()
main_frame.title("I'm bored pls Help")
main_frame.attributes("-fullscreen", True)
counter_l = Label(text=counter)
random_colour_b = Button(
main_frame, bg="black", fg="gold", text="Click Me!", font="Arial 20",
command=lambda: main_frame.config(background=choice(["black", "white"]))
)
exit_b = Button(text="Kill Me!", bg="black", fg="red", font="Arial 20", command=main_frame.destroy)
counter_l.pack()
random_colour_b.pack()
exit_b.pack()
main_frame.mainloop()
overlord()