最近一直在做学校的课题,然后在解决一个小需求时碰到了问题。
我的advisior希望我可以在用户按下了最后那个正确的按钮后出现一个gif。但是因为使用了tkinter模块,好像也没法直接实现,所以我准备把gif放在另一个py文件里,在主文件里引入那个文件的一个类(像下面那样)。然后在主文件里写个函数定义出gif图片的路径并加载文件。
from Win import ImageLabel
主文件里的app是SeaofBTCapp,但是当我把gif函数写好后,程序死也不运行,总是提示nameerror,错误信息是name 'app' is not defined。stack overflow上面的人说因为缩进不对,可以看看我的缩进(我觉得是对的):
这段是gif函数
def gif():
top = tk.Toplevel(app)
lbl = ImageLabel(top)
lbl.pack()
lbl.load('D:/Personal/Game/Win.gif')
这段是按钮的类
class Success(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Yay! You have passed my chemistry challenge! Would you like to continue?",
font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self, text="Continue", command=[gif(), success1()])
button1.pack()
button2 = tk.Button(self, text="Quit", command=lambda: controller.destroy())
button2.pack()
def correspondingBehavior(self, choice): # Comment
print(choice)
这段是app变量的定义,它是SeaofBTCapp类的
class SeaofBTCapp(tk.Tk): # Define a class for the tk windows
def __init__(self, *args, **kwargs): # Base properties of tk windows
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (
StartPage, MCQ, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10, Failure, Success,
Credits): # TODO: add the class number into the for loop here
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
app = SeaofBTCapp()
app.mainloop()
所以是不是缩进搞错了?
1