Lellansin Lellansin - 1 year ago 90
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.

Answer Source

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

print("the foreground of bluebutton is", bluebutton.cget("fg"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download