Newb Tkinter Question: Object has no attribute 'tk'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • python programming newb

    #1

    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(Fra me):
    """The full screen window with menu"""
    def __init__(self, master):

    Frame.__init__( self, master)
    self.grid()
    self.create_men u()
    self.create_wid get()

    def create_menu(sel f):
    """Create file and help menu"""
    #Create Filemenu
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_co mmand(label = "Exit", command = root.quit)
    menubar.add_cas cade(label="Fil e", menu=filemenu)
    #Create Helpmenu
    helpmenu = Menu(menubar, tearoff=0)
    helpmenu.add_co mmand(label="Ab out", command=hello)
    menubar.add_cas cade(label="Hel p", menu=helpmenu)

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


    class NewWindow(objec t):

    def __init__(self):
    self.z = Toplevel()
    self.z.geometry ("640x480")
    self.z.title("T his is the New Window")
    self.frame = Frame(self.z)
    self.create_wid getnew()
    self.frame.grid ()

    def create_widgetne w(self):
    self.lbl = Label(self, text = "Here is a label")
    self.lbl.grid(r ow = 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("ROO T WINDOW")
    root.geometry(" 300x200")
    menubar = Menu(root)
    root.config(men u=menubar)
    mainapp=Applica tion(root)
    root.mainloop()

  • Simon Forman

    #2
    Re: Newb Tkinter Question: Object has no attribute 'tk'

    python programming newb wrote:[color=blue]
    > 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:[/color]


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

    ~Simon

    Comment

    • Simon Yau

      #3
      Re: Newb Tkinter Question: Object has no attribute 'tk'

      python programming newb wrote:[color=blue]
      >
      > 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(objec t):
      >
      > def __init__(self):
      > self.z = Toplevel()
      > self.z.geometry ("640x480")
      > self.z.title("T his is the New Window")
      > self.frame = Frame(self.z)
      > self.create_wid getnew()
      > self.frame.grid ()
      >
      > def create_widgetne w(self):
      > self.lbl = Label(self, text = "Here is a label")
      > self.lbl.grid(r ow = 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...][/color]

      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

      Comment

      Working...