I know how to change a label's color with a command attached to a button, but I want to do it programmatically, based on a variable's value.
If an error message is sent to the build_window
function, I want to display the error message on a label with a red background, and if a non-error message is sent, no message is to be displayed and no color changes are expected.
In my code, I call the function to build a tkinter window two times. The first time, I pass it a non-error message, the second time, an error message.
My code works for displaying the error message when expected, the problem is only with changing the background color of the error message label.
If I un-comment the l1.config(bg="red")
command shown, I get this error when passing an error message to build_window()
:
UnboundLocalError: local variable 'l1' referenced before assignment
on the l1.config(bg="red")
command.
If I move the entire if structure to just before mainloop()
, I get this error even when passing the non-error message:
UnboundLocalError: local variable 'error_message' referenced before assignment
on the l1=Label(root,text = error_message)
command.
If I add global l1
and global error_message
to build_window()
, the error I get is this:
tkinter.TclError: invalid command name ".!label"
I also tried just initially defining the label with bg="red"
, hoping that when I send a zero-length string, it would still be gray, but it displays a bit of red in the middle of the l1 label.
I've been writing simple python for a while, but I'm new to GUI apps and tkinter just confuses me every time I try something.
I've searched for a solution but could not find anything addressing changing a window without using a command attached to a button.
Any advice or clarification would be greatly appreciated!
from tkinter import Tk, IntVar, Label, mainloop, Button
def build_window(incoming_error_message) :
if incoming_error_message == "initial value" :
error_message = ""
else :
#l1.config(bg="red")
error_message = incoming_error_message
def quitHandler():
root.destroy()
root = Tk()
l1=Label(root,text = error_message)
l1.grid(row=0,column=0)
quitButton = Button(root, text="To end, click here",command=quitHandler)
quitButton.grid(row=1,column=0)
mainloop()
def call_build_window() :
build_window("initial value")
build_window("Error!")
call_build_window()