tkinter: making widgets instance or not?
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?
import Tkinter as tk
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)
self.label = tk.Label(frame, text=label_text, width=10)
self.entry = tk.Entry(frame)
root = tk.Tk()
app = App(root)
More information about the Python-list