473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 14799
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.