Newb Tkinter Question: Object has no attribute 'tk'

  • Thread starter python programming newb
  • Start date
P

python programming newb

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()
 
S

Simon Forman

python said:
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
 
S

Simon Yau

python said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top