Lellansin Lellansin - 20 days ago 7x
Python Question

How can I get a Tkinter widget attribute?

I create a Button, and set its callback, but how can I get the attribute of the button like


from Tkinter import *

def callback(self):
tkMessageBox.showinfo("button", color)

top = Tk()
frame = Frame(top)

greenbutton = Button(frame, text="Brown", fg="brown", command=callback)
greenbutton.pack( side = RIGHT )

bluebutton = Button(frame, text="Blue", fg="blue", command=callback)
bluebutton.pack( side = LEFT )


I just want when I click the button blue and it will tell me it is blue.


Every widget has a method, cget, which you can use to get configured values:

print("the foreground of bluebutton is", bluebutton.cget("fg"))