2015-03-31 16 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像你這樣。

相關問題