Я не уверен, что не так с моим кодом, но всякий раз, когда я пытаюсь запустить его, я получаю следующую ошибку:
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
Я знаю, что это должно быть проблема с аспектом ООП кодатак как у меня очень похожая не-ООП-версия этого кода, которая прекрасно работает без ошибок:
#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()
Это моя версия, созданная с ООП, которая выдает мне ошибку: (У меня есть все мои файлы изображенийв каталоге, где также сохранен мой код.)
#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, поэтому я приношу свои извинения, если это очень простая ошибка.