Skip to content Skip to sidebar Skip to footer

Tkinter Troubles - Name Frame Is Not Defined

import Tkinter class Application(Frame): def __init__(self, master): Frame.__init__(self,master) self.grid() self.CreateWidgets() def CreateWidgets

Solution 1:

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):

fromTkinterimport *

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:
    # Python2import Tkinter as tk 
except ImportError:
    # Python3import tkinter as tk 

classApplication(tk.Frame):
    def__init__(self, master):
        tk.Frame.__init__(self,master)
        self.grid()
        self.CreateWidgets()
    defCreateWidgets(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)

Solution 2:

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:

fromTkinterimportFrame

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:

importTkinteras 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

Solution 3:

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

Solution 4:

I had this same error too. The issue with mine as 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.

Post a Comment for "Tkinter Troubles - Name Frame Is Not Defined"