tkinter NameErrorFrame is not defined

by: nomadsoul, 7 years ago

Last edited: 7 years ago

When I try to use the tkinter module for creating a text and graphic I always get NameError: name 'Frame' is not defined. What is frustrating is that it worked earlier but now has the error. Am using Python 3 + and Anaconda3.
I'm doing the code from the basic tutorial but I have also used  other code from the net. The _init_.py file is where it should be on my machine: C:UsersIXAnaconda3Libtkinter. I'm using spyder but it get the same error with Jupyter notebook.
All other modules are working fine and I downloaded pillow with no problems.
Here is the code from the tutorial, I removed the comments: Any suggestions or thoughts are appreciated.
<pre class='prettyprint lang-py'>
from tkinter import *
from PIL import Image, ImageTk

class Window(Frame)  
    def __init__(self, master=None)  
        Frame.__init__(self, master)  
        self.master = master
        self.init_window()
    def init_window(self)    
        self.master.title("GUI"
        self.pack(fill=BOTH, expand=1)  
        menu = Menu(self.master)
        self.master.config(menu=menu  
        file = Menu(menu)    
        file.add_command(label="Exit", command=self.client_exit)  
        menu.add_cascade(label="File", menu=file)      
        edit = Menu(menu)
        edit.add_command(label="Show Img", command=self.showImg)
        edit.add_command(label="Show Text", command=self.showText)    
        menu.add_cascade(label="Edit", menu=edit)
    def showImg(self):
        load = Image.open("chat.png")
        render = ImageTk.PhotoImage(load)      
        img = Label(self, image=render)
        img.image = render
        img.place(x=0, y=0)

    def showText(self):
        text = Label(self, text="Hey there good lookin!")
        text.pack()
    def client_exit(self):
        exit()

root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()  
<pre class='prettyprint lang-py'>



You must be logged in to post. Please login or register an account.



I made a stupid mistake by pasting the tutorial code in the _ini_py file without realizing it and that is why I got the error.  Sorry for that. I just have to find a new file to replace it.  

-nomadsoul 7 years ago

You must be logged in to post. Please login or register an account.