2013-11-14 3 views
0

Я создаю кнопку и устанавливаю ее обратный вызов, но как я могу получить атрибут кнопки, например, fg?Как я могу получить атрибут виджета Tkinter?

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() 

Я просто хочу, когда я нажму кнопку синего цвета, и он скажет мне, что это синий цвет.

+0

Вы можете получить его RGB значения с помощью 'winfo_rgb' – Gogo

+0

Для других informtion вы можете использовать' данные() ' – Gogo

ответ

3

Каждый виджет имеет метод, cget, который вы можете использовать, чтобы получить настроенные значения:

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

спасибо, но это не то, что я хочу. – Lellansin

+0

@Lellansin: почему это не то, что вы хотите? Вы хотели получить цвет, это получает цвет. Неужели вы не знаете, как подключить его к кнопке? –

0

Я полагаю, Lellansin просит для этого:

def callback(event): 
    obj=event.widget 
    name=str(obj) 
    print("the foreground of %s is %s" %(name,obj.cget("fg"))) 
1

За год с опозданием, но Я думаю, что это то, что спрашивают о:

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()) 

что приводит к:

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

Смежные вопросы