Unnamed Tkinter object reference?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Tue May 14 16:14:25 EDT 2002


On Tuesday 14 May 2002 4:44 pm, you wrote:
> Hello, Python experts!
>
> I have an app where I'm building widgets on the fly, and they do not
> have names, so I can't refer to them to use their methods, but it would
> be useful to be able to get some sort of temporary handle for a widget
> to be able to get/set parameters for it.
>

Here is one take on it:-

import calendar, Tkinter



class TkCalendar(Tkinter.Frame):
    def __init__(self, parent, year=2002, month=5):
        Tkinter.Frame.__init__(self, parent) 
        data=calendar.monthcalendar(year, month)
        
        col=0
        for day in calendar.day_abbr:
            Tkinter.Label(self, text=day).grid(row=0, col=col, sticky='nsew')
            col+=1
        row=1
        for data_row in data:
            col=0
            for day in data_row:
                if day==0: 
                    col+=1
                    continue
                b=Tkinter.Button(self, text=day, command=lambda self=self,
                    day=day: self.close(day))
                b.grid(row=row, col=col, sticky='nsew')
                col+=1
            row+=1
        
        self.pack()
        
        
    def close(self, day):
        print day
        self.quit()
        
if __name__=='__main__':
    root=Tkinter.Tk()
    cal=TkCalendar(root)
    root.mainloop()



HTH
Martin





More information about the Python-list mailing list