Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a Tkinter widget attribute?

Tags:

python

tkinter

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.

like image 454
Lellansin Avatar asked Nov 14 '13 08:11

Lellansin


Video Answer


3 Answers

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

print("the foreground of bluebutton is", bluebutton.cget("fg"))
like image 123
Bryan Oakley Avatar answered Oct 30 '22 05:10

Bryan Oakley


Over a year late, but I think this is what's being asked for:

import tkinter as tk


class GetWidgetAttributes:
    @staticmethod
    def get_attributes(widget):
        widg = widget
        keys = widg.keys()
        for key in keys:
            print("Attribute: {:<20}".format(key), end=' ')
            value = widg[key]
            vtype = type(value)
            print('Type: {:<30} Value: {}'.format(str(vtype), value))


if __name__ == '__main__':
    gw = GetWidgetAttributes()
    # For Example, find all attributes of Tkinter Frame
    gw.get_attributes(tk.Frame())

Which results in:

Attribute: bd                   Type: <class 'int'>                  Value: 0
Attribute: borderwidth          Type: <class 'int'>                  Value: 0
Attribute: class                Type: <class 'str'>                  Value: Frame
Attribute: relief               Type: <class 'str'>                  Value: flat
Attribute: background           Type: <class 'str'>                  Value: SystemButtonFace
Attribute: bg                   Type: <class 'str'>                  Value: SystemButtonFace
Attribute: colormap             Type: <class 'str'>                  Value: 
Attribute: container            Type: <class 'int'>                  Value: 0
Attribute: cursor               Type: <class 'str'>                  Value: 
Attribute: height               Type: <class 'int'>                  Value: 0
Attribute: highlightbackground  Type: <class 'str'>                  Value: SystemButtonFace
Attribute: highlightcolor       Type: <class 'str'>                  Value: SystemWindowFrame
Attribute: highlightthickness   Type: <class 'int'>                  Value: 0
Attribute: padx                 Type: <class '_tkinter.Tcl_Obj'>     Value: 0
Attribute: pady                 Type: <class '_tkinter.Tcl_Obj'>     Value: 0
Attribute: takefocus            Type: <class 'str'>                  Value: 0
Attribute: visual               Type: <class 'str'>                  Value: 
Attribute: width                Type: <class 'int'>                  Value: 0

Larz60p

like image 30
Larz60p Avatar answered Oct 30 '22 06:10

Larz60p


I suppose, Lellansin is asking for this:

 def callback(event):
    obj=event.widget
    name=str(obj)
    print("the foreground of %s is %s" %(name,obj.cget("fg")))
like image 42
Iordanov K. Avatar answered Oct 30 '22 05:10

Iordanov K.