在python中,程序运行良好

2024-04-26 01:39:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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

Tags: runinpyimageimportselfinit错误
1条回答
网友
1楼 · 发布于 2024-04-26 01:39:28
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。你知道吗

相关问题 更多 >