472,356 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

Newb Tkinter Question: Object has no attribute 'tk'

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
2 14493
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
2
by: John Pote | last post by:
Running my programme in Python 2.3.4 I received the following msg in the consol :- (Pent III running W2K prof) """ Exception in Tkinter callback Traceback (most recent call last): File...
4
by: Ron Provost | last post by:
Hello, I'm using python 2.4.2 on Win XP Pro. I'm trying to understand a behavior I'm seeing in some Tkinter code I have. I've reduced my question to a small piece of code: #####BEGIN CODE...
1
by: yvesd | last post by:
hello i want to intercept tkinter python system events like wm_delete_window and if possible for any window, but the newest code I've produced give me an error : Traceback (most recent call...
5
by: H J van Rooyen | last post by:
Hi, I am struggling to get the pack method to do what I intend. I am trying to display user input in a seperate window, along with a little description of the field, something like this: ...
2
by: BartlebyScrivener | last post by:
Finally started trying to build a simple gui form for inserting text data into a mysql db of quotations. I found this nice Tkinter tutorial, http://www.ibiblio.org/obp/py4fun/gui/tkPhone.html...
3
by: seanacais | last post by:
I'm trying to build an unknown number of repeating gui elements dynamically so I need to store the variables in a list of dictionaries. I understand that Scale "variable" name needs to be a...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
0
by: lee.walczak | last post by:
I actually post a topic relating to my problem here: (http://groups.google.co.uk/group/comp.lang.python/browse_thread/ thread/a073d532c4481bfe?hl=en# ) But I thought it could be useful to...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.