2015-03-31 15 views
-1

我已经创建了一个使用Python的小型Windows应用程序。我的代码在几分钟前运行。现在它显示出一些错误。我非常担心这是怎么发生的。片刻之前的代码工作正常。现在出现了错误。我的代码: -Python中的名称错误,程序工作很好时刻

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 
+0

感谢每一个。我已经解决了这个问题。该问题可能是由于某些名称空间解析错误。通过添加:tkinter import * – 2015-03-31 13:04:51

+2

_adding_'from tkinter import *'不能正确修复,因为您有两种不同的导入tkinter的方法。这会导致代码非常混乱。 – 2015-03-31 16:07:07

+0

我可以知道你为什么降低我的问题吗?我对Python非常陌生,所以如果代码中有任何错误代替减速,请帮助我知道。 – 2015-04-02 06:43:16

回答

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

您应该预先Frametk(就像上图),或使用from tkinter import *,然后调用Frame像你这样。

相关问题