tkinter: making widgets instance or not?

John Salerno johnjsal at NOSPAMgmail.com
Tue Jun 6 21:42:18 CEST 2006


Fredrik Lundh wrote:

> however, if you need to access a widget later on, it might be a good 
> idea to save a reference to it somewhere...

To follow up on that point, I have the following code now. I have two 
questions about it:

1. Can I somehow make the passing of 'master' to the draw_entry method 
automatic, so I don't have to type it each time I call the function?

2. Related to your comment, the obvious problem here is that it doesn't 
save references to the text box names, so I can't access them later. Is 
there still a way to automate the process like I've done, but have each 
entry field have a separate name?

Thanks.

---------------

import Tkinter as tk


class App:

     def __init__(self, master):
         self.draw_entry(master, 'First Name:')
         self.draw_entry(master, 'Last Name:')

     def draw_entry(self, master, label_text):
         frame = tk.Frame(master, bd=4)
         frame.pack()
         self.label = tk.Label(frame, text=label_text, width=10)
         self.label.pack(side=tk.LEFT)
         self.entry = tk.Entry(frame)
         self.entry.pack(side=tk.LEFT)


root = tk.Tk()
app = App(root)
root.resizable(width=False, height=False)
root.mainloop()



More information about the Python-list mailing list