I’m having an issue trying to use the destroy() method in python.
I have created a class and was trying to make a window that would bring me to different frames, and destroy the ones I was on before.
from tkinter import *
import sys, os
class Interface:
def startGame(self):
frame.destroy()
frame2 = Frame(window,background = "black")
frame2.place(x=100,y=220)
entry = Entry(frame2, bg="black",fg = "white",font=("Comic Sans", 30,"underline"))
entry.pack()
login = Button(frame2,text="Login")
login.pack()
cancel = Button(frame2,text="Cancel",command = window.mainloop)
cancel.pack()
def loadGame():
pass
def firstWindow(self):
frame = Frame(window)
picture = PhotoImage(file="dinossaur.png")
label = Label(frame,
text="Final Project: Ultimate",
font=("Comic Sans", 40, "italic"),
fg="gray",
background = "maroon",
#relief = RAISED, bd = 10, padx = 9, pady = 9
image=picture,
compound = "bottom")
frame.pack()
start = Button(frame, text="Start Game", command =self.startGame)
load = Button(frame,text="Load Game", command = self.loadGame)
exitB = Button(frame,text="Exit",command = window.destroy)
label.pack()
start.place(x=220,y=240)
load.place(x=220,y=270)
exitB.place(x=220,y=300)
# to change icon on top left!
##icon = image(file="logo.png")
##window.iconphoto(True, icon)
window = Tk()
window.geometry("640x480")
window.title("Final Project: Ultimate")
window.config(background="maroon")
game = Interface()
game.firstWindow()
window.mainloop()
But whenever I run it, I find a couple of issues:
the image is not showing, and startGame() gives me the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File “C:UsersbetoiAppDataLocalProgramsPythonPython312Libtkinter_init_.py”, line 1962, in call
return self.func(*args)
^^^^^^^^^^^^^^^^
File “C:/Users/betoi/OneDrive/Área de Trabalho/finalProject/interface.py”, line 5, in startGame
frame.destroy()
^^^^^
NameError: name ‘frame’ is not defined. Did you mean: ‘Frame’?
Joao Roberto Santos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.