Lellansin Lellansin - 2 months ago 14
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

fg
?

from Tkinter import *

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

top = Tk()
frame = Frame(top)
frame.pack()

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 )

top.mainloop()


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

Answer

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

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