Tkinter troubles – Name frame is not defined

Question:

import Tkinter

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()
        self.CreateWidgets()
    def CreateWidgets(self):
        self.LoginButton = Button(Self)
        self.LoginButton["text"] = "Login"
        self.LoginButton.grid()
        self.QUIT_Button = Button(self)
        self.QUIT_Button["text"] = "Quit"
        self.QUIT_Button["command"] = self.quit
        self.QUIT_Button["fg"] = "red"

root = Tk()
root.title("Login")
root.geometry("500x500")
app = Application(root)
root.mainloop()

This is the youtube tutorial that I have been following: https://www.youtube.com/watch?v=YCLTv6wh3jE&index=39&list=PLB0701884E5AE1B45

And this is the error that keeps occurring:

Traceback (most recent call last):
  File "C:UsersomerDesktoptest.py", line 3, in <module>
    class Application(Frame):
NameError: name 'Frame' is not defined

I am a complete noob at Python and am still learning so any help would be appreciated.

Asked By: Raptor22

||

Answers:

You have to import Frame in order to use it like you are. As it stands you have imported Tkinter, but that doesn’t give you access to Frame, Button or Tk the way you’ve used them. but you either need to do:

from Tkinter import Frame

or

from Tkinter import * (* means ‘all’ in this case, though this isn’t necessary when only using a few modules)

or you could leave your import statement as is(import Tkinter) and change your code like so:

class Application(Tkinter.Frame):

and

self.LoginButton = Tkinter.Button(Self)

However, I would recommend that if you do this, you do:

import Tkinter as tk

That way, you can do tk.Frame and tk.Button etc.

For any modules that you want to use from Tkinter you need to import them also in the same fashion.

You can do single line imports like so:

from Tkinter import Tk, Frame, Button etc.

Check out this info on importing in Python: http://effbot.org/zone/import-confusion.htm

Answered By: Totem

You need to import Frame, Button, Tk.

You can either explicitly import all of them from Tkinter:

from Tkinter import Frame, Button, Tk

or import everything from Tkinter (which is not a good thing to do):

from Tkinter import *

or leave your import as is (import Tkinter) and get Frame, Button and Tk from the Tkinter namespace, e.g. for Frame:

class Application(Tkinter.Frame):

Even better would be to import tkinter in a universal way that would work for both python2 and python3:

try:
    # Python2
    import Tkinter as tk 
except ImportError:
    # Python3
    import tkinter as tk 

class Application(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self,master)
        self.grid()
        self.CreateWidgets()
    def CreateWidgets(self):
        self.LoginButton = tk.Button(self)
        self.LoginButton["text"] = "Login"
        self.LoginButton.grid()
        self.QUIT_Button = tk.Button(self)
        self.QUIT_Button["text"] = "Quit"
        self.QUIT_Button["command"] = self.quit
        self.QUIT_Button["fg"] = "red"

root = tk.Tk()
root.title("Login")
root.geometry("500x500")
app = Application(root)
root.mainloop()

Also, you have a typo, replace (watch Self):

self.LoginButton = Button(Self)

with:

self.LoginButton = Button(self)
Answered By: alecxe

Frame Tk, and Button are all located in the Tkinter namespace. Thus, you have to qualify them to let Python know where they are1:

import Tkinter

class Application(Tkinter.Frame):
...
        Tkinter.Frame.__init__(self, master)
...
        self.LoginButton = Tkinter.Button(self)
...
        self.QUIT_Button = Tkinter.Button(self)
...

root = Tkinter.Tk()

That, or you could just import the names directly:

from Tkinter import Frame, Tk, Button

1If you decide to use this first solution, it would probably be best to import Tkinter like this:

import Tkinter as tk

That way, the code becomes this:

import Tkinter as tk

class Application(Tkinter.Frame):
...
        tk.Frame.__init__(self, master)
...
        self.LoginButton = tk.Button(self)
...
        self.QUIT_Button = tk.Button(self)
...

root = tk.Tk()

which is a lot more brief.

Answered By: user2555451

Well it is a bit late but for someone having same error, be sure there is no tkinter.py file in the folder.

Answered By: Ali Akgöz

I had this same error too. The issue with mine is I had a file called tkinter.py and that was overriding the built-in file tkinter. So to fix it, I changed my filename to something else.

Answered By: Olusola Caleb
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.