Newb Tkinter Question: Object has no attribute 'tk'

Simon Yau simonyau at gmail.com
Sat Jul 1 02:16:29 EDT 2006


python programming newb wrote:
>
> I'm new to python and tkinter.  I'm trying to write a program that
> opens the root window with a button that then opens a toplevel window
> that then has it's own widgets.  I can get the new toplevel window to
> open but none of the widgets appear. The console gives me:
>
> AttributeError: 'NewWindow' object has no attribute 'tk'
>
> Here's my code:
>
> [snipped...]
>
> class NewWindow(object):
>
> 	def __init__(self):
> 		self.z = Toplevel()
> 		self.z.geometry("640x480")
> 		self.z.title("This is the New Window")
> 		self.frame = Frame(self.z)
> 		self.create_widgetnew()
> 		self.frame.grid()
>
> 	def create_widgetnew(self):
> 		self.lbl = Label(self, text = "Here is a label")
> 		self.lbl.grid(row = 0, column = 1, sticky = N)
> 		self.bttn = Button(self, text = "Close", command=self.z.quit)
> 		self.bttn.grid(row = 2, column = 1, sticky = S)
>  [snipped...]

You're referencing self (object) instead of a Frame when creating the
Label. Change self to self.frame in both the Label and Button
initialization and it'll work.

Hope it helps,
Simon




More information about the Python-list mailing list