By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,359 Members | 2,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,359 IT Pros & Developers. It's quick & easy.

Newb Tkinter Question: Object has no attribute 'tk'

P: n/a
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()

Jun 30 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
python programming newb wrote:
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:

Please post the entire traceback, not just the error message, thanks.

~Simon

Jun 30 '06 #2

P: n/a
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

Jul 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.