jumbi533
jumbi533

Reputation: 71

Getting the value of the bg property of a Label in Tkinter

I have been learning ui programming with Tkinter and I have gotten pretty far. I can configure/change text, fg, and bg properties of a Label, but I don't know how to obtain the bg property of the Label.

Is there some way I could save the bg value of a Tkinter Label into a variable, in order to compare it with other values?

Upvotes: 1

Views: 1094

Answers (2)

MikeVaughan
MikeVaughan

Reputation: 1501

You can use cget():

label.cget('background')

or you can treat your label like a dictionary:

label['background']

Example:

from Tkinter import *

main = Tk()
l = Label(main, text = "Label", background = "lime")
l.pack()

if l["background"] == 'lime':
    print "Lime!"
if l.cget("background") == 'lime':
    print "Still Lime!"

main.mainloop()

Console output:

Lime!
Still Lime!

Upvotes: 3

Kevin
Kevin

Reputation: 76194

You can use the cget method to get the value of a widget's attributes. Example:

if my_widget.cget("background") == "red":
    print "The widget is red"

Upvotes: 3

Related Questions