Newb Tkinter Question: Object has no attribute 'tk'

python programming newb james.ryland.miller at gmail.com
Fri Jun 30 16:33:57 EDT 2006


Hi all, first post.

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:

####
from Tkinter import *


class Application(Frame):
	"""The full screen window with menu"""
	def __init__(self, master):

		Frame.__init__(self, master)
		self.grid()
		self.create_menu()
		self.create_widget()

	def create_menu(self):
		"""Create file and help menu"""
		#Create Filemenu
		filemenu = Menu(menubar, tearoff=0)
		filemenu.add_command(label = "Exit", command = root.quit)
		menubar.add_cascade(label="File", menu=filemenu)
		#Create Helpmenu
		helpmenu = Menu(menubar, tearoff=0)
		helpmenu.add_command(label="About", command=hello)
		menubar.add_cascade(label="Help", menu=helpmenu)

	def create_widget(self):
		"""Make a button in the center that when pushed opens another
window"""
		self.bttn1 = Button(self, text = "Push to open new window",
command=NewWindow)
		self.bttn1.grid()


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)

#Main

def hello():
	print "hello in the terminal"

root = Tk()
root.title("ROOT WINDOW")
root.geometry("300x200")
menubar = Menu(root)
root.config(menu=menubar)
mainapp=Application(root)
root.mainloop()




More information about the Python-list mailing list