在python中的NameError,程序在很久以前工作得很好

时间:2015-03-31 12:58:16

标签: python tkinter

我使用python创建了一个小型Windows应用程序。我的代码刚刚运行。现在它显示出一些错误。我很了解这是怎么发生的。片刻之前代码工作正常。现在出现了错误。我的代码: -

import tkinter as tk
from PIL import ImageTk

class MyApp(Frame):
def __init__(self,parent):
    Frame.__init__(self,parent)
    self.master.title("Music Library")
    self.parent=parent
    self.images = []
    self.createUI()


def createUI(self):
    self.grid()
    raw_data=Image.open("pic.jpg")
    image = ImageTk.PhotoImage(raw_data)
    label=tk.Label(image = image)
    self.images.append(image)
    label.grid(column=0,row=0)
    btn = tk.Button(text="Click Me")
    btn.grid(column=0,row=0)


root=tk.Tk()
app=MyApp(root)
app.mainloop()

错误是:

Traceback (most recent call last):
  File "C:\Python34\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python34\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python34\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 654, in run
    exec(cmd, globals, locals)
  File "D:\DeepakK\Python programs\Background Image.py", line 1, in <module>
    import tkinter as tk
NameError: name 'Frame' is not defined

1 个答案:

答案 0 :(得分:2)

class MyApp(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(self,parent)
        self.master.title("Music Library")
        self.parent=parent
        self.images = []
        self.createUI()

您应在Frame前加tk(如上所述),或使用from tkinter import *,然后像您一样致电Frame