VladPotrosky wrote:
> Hello all.
> I am writing a project which will execute a method of each widget in a form,
> based on what type the widget is (ie: executing .deselect() if it's a
> checkbox, .delete() if it's an entry ect...) 
> i was basically duck typing all the widgets, assuming that if trying a
> certain method worked, then it was the type i wanted (running .deselect()
> successfully meant that i must have a checkbox)
> unfortunately, i realized that many widgets have methods in common, and that
> this approach didn't work in some cases.
> is there a good way to determine the type of a widget which is not duck
> typing?

Use the winfo_class() method (Example below):




from Tkinter import *
r = Tk()
b = Button(r, text='test')
b.pack()
c = Checkbutton(r)
c.pack()
e = Entry(r)
e.pack()

widgets = [b,c,e]

def widgetMethod(widget):
    if widget.winfo_class() == 'Button':
        print 'A Button widget!'
    elif widget.winfo_class() == 'Checkbutton':
        print 'A Checkbutton widget!'
    elif widget.winfo_class() == 'Entry':
        print 'An Entry widget!'

for widget in widgets:
    widgetMethod(widget)

r.mainloop()


Regards,

John
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to