我不确定代码有什么问题,但是每当我尝试运行它时,都会出现以下错误:
File "L:\Year 12 and 13\Computer Science\NEA\30.11.18\GUI TKINTER\no cont.py", line 80, in __init__
button3 = tk.Button(self, command=lambda: controller.MusicClick(), image = musicPic, text="Music", fg="Orange",font="none 20").place(x=30, y=640)
File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 2363, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 2293, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist
我知道代码的OOP方面一定有问题,因为我有一个非常相似的非OOP版本的代码,可以完美地工作且没有错误:
#Importing GUI
from tkinter import *
#Window Properties
window = Tk()
window.geometry('1280x720')
window.configure(background="blue")
#Declaring
SFXisMuted = False
MusicisMuted = False
#Title
Label (window, text="Connect 4 - Made by Luke Petrocochino", bg="blue", fg="white", font="Comic_Sans 40 bold").place(x=150,y=150)
#Pictures
musicPic = PhotoImage(file="musicalnoteresize.gif")
musicPicMUTED =PhotoImage(file="musicalnoteresizeMUTED.gif")
SFXPic = PhotoImage(file="SFXresize.gif")
SFXPicMUTED = PhotoImage(file="SFXresizeMUTED.gif")
#Command Sub Routines
def SFXClick():
global SFXisMuted
if SFXisMuted == False:
button4 = Button(command=SFXClick, text="SFX", image=SFXPicMUTED, fg="Orange",font="none 20").place(x=110, y=640)
SFXisMuted = True
else:
button4 = Button(command=SFXClick, text="SFX", image=SFXPic, fg="Orange",font="none 20").place(x=110, y=640)
SFXisMuted = False
def MusicClick():
global MusicisMuted
if MusicisMuted == False:
button3 = Button(command=MusicClick, text="Music",image=musicPicMUTED, fg="Orange",font="none 20").place(x=30, y=640)
MusicisMuted = True
else:
button3 = Button(command=MusicClick, text="Music",image=musicPic, fg="Orange",font="none 20").place(x=30, y=640)
MusicisMuted = False
def CloseWindow():
window.destroy()
exit()
#Buttons
button1 = Button(text ="Play!", font="none 60", fg= "Green").place(x=550, y=280)
button2 = Button(command=CloseWindow, text="Exit ",font="none 20", fg="Red").place(x=1175, y=640)
button3 = Button(command=MusicClick, text="Music",image=musicPic, fg="Orange",font="none 20").place(x=30, y=640)
button4 = Button(command=SFXClick, text="SFX", image=SFXPic, fg="Orange",font="none 20").place(x=110, y=640)
#End
window.mainloop()
这是我使用OOP制作的版本,该错误提示我:(我的所有图像文件也保存在保存代码的目录中。)
#Importing GUI
import tkinter as tk
app = tk.Tk()
LARGE_FONT= ("Verdana", 12)
class Connect4(tk.Tk):
def __init__(self, *args, **kwargs):
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 = {}
SFXisMuted = False
MusicisMuted = False
for F in (StartPage, PageOne, PageTwo):
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()
def SFXClick(self):
global SFXPic
global SFXisMuted
if SFXisMuted == False:
button4 = Button(command=SFXClick, text="SFX", image=SFXPicMUTED, fg="Orange",font="none 20").place(x=110, y=640)
SFXisMuted = True
else:
button4 = Button(command=SFXClick, text="SFX", image=SFXPic, fg="Orange",font="none 20").place(x=110, y=640)
SFXisMuted = False
print("it works")
def MusicClick(self):
global MusicisMuted
global musicPic
if MusicisMuted == False:
button3 = Button(command=lambda: controller.MusicClick, text="Music",image=musicPicMUTED, fg="Orange",font="none 20").place(x=30, y=640)
MusicisMuted = True
else:
button3 = Button(command=MusicClick, text="Music",image=musicPic, fg="Orange",font="none 20").place(x=30, y=640)
MusicisMuted = False
def CloseWindow(self):
Connect4.destroy(self)
exit()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
musicPic = tk.PhotoImage(file="musicalnoteresize.gif")
SFXPic = tk.PhotoImage(file="SFXresize.gif")
label = tk.Label(self, text="Connect 4 - Made by Luke Petrocochino", bg="blue", fg="white", font="Comic_Sans 40 bold").place(x=150,y=150)
button1 = tk.Button(text ="Play!", font="none 60", fg= "Green").place(x=550, y=280)
button2 = tk.Button(self, command=lambda: controller.CloseWindow(), text="Exit ",font="none 20", fg="Red").place(x=1175, y=640)
button3 = tk.Button(self, command=lambda: controller.MusicClick(), image = musicPic, text="Music", fg="Orange",font="none 20").place(x=30, y=640)
button4 = tk.Button(self, command=lambda: controller.SFXClick(), image = SFXPic, text="SFX", fg="Orange",font="none 20").place(x=110, y=640)
button3.image = musicPic
button4.image = SFXPic
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One", font=LARGE_FONT)
label.pack(pady=10,padx=10)
buttonNav = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
buttonNav.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two", font=LARGE_FONT)
label.pack(pady=10,padx=10)
buttonNav = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
buttonNav.pack()
button2 = tk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button2.pack()
app = Connect4()
app.geometry('1280x720')
app.configure(background="blue")
app.mainloop()
我是Tkinter的OOP初学者,所以如果这是一个非常简单的错误,我深表歉意。
答案 0 :(得分:0)
这很难找到。通过将按钮传递到place()
来创建按钮时,您经常犯错误。 Place将返回None
,这意味着您将没有对按钮的引用。
button3 = tk.Button(self, command=lambda: controller.MusicClick(),
image = musicPic, text="Music",
fg="Orange", font="none 20").place(x=30, y=640)
相反,首先创建按钮,然后将其放置在以下位置:
button3 = tk.Button(self, command=lambda: controller.MusicClick(),
image = musicPic, text="Music",
fg="Orange", font="none 20")
button3.place(x=30, y=640)
否则,您将无法将图像保存到按钮:
button3.image = musicPic
然后,您在创建根窗口时也犯了一个不寻常的错误:
app = tk.Tk()
,并使用相同的名称命名应用程序:
app = Connect4()
这有效果,但是我不知道为什么会这样,但是效果是该按钮找不到您要传递给它的图像。如果只删除第一个app = tk.Tk()
,一切正常。
也;在MusicClick()
中的函数Connect4
中,您要创建几个变量global
。对我来说,这很奇怪,因为OOP的好处之一就是您不需要全局变量。我将使它们成为实例变量。