I made a game about guess numbers. After repeated attempts, I still couldn’t change a problem.
The code is as follows:
#
#
import tkinter
import random
import pygame
#
pygame.init()
#
root = tkinter.Tk()
#
bg = tkinter.Canvas(root, width=1000, height=750)
bg.pack()
bg_p = tkinter.PhotoImage(file="bg.png")
bg.create_image(500, 375, image=bg_p)
#
bgm = pygame.mixer.Sound("bgm.mp3")
bgm.play(-1)
#
low = 0
high = 101
#
random.randrange(low, high)
#
def onclick():
#
global low, high
#
guess = int(input("Guess a number: "))
#
if guess == answer:
#
bg = tkinter.Canvas(root, width=1000, height=750)
bg.pack()
bg_p = tkinter.PhotoImage(file="bingo.png")
bg.create_image(500, 375, image=bg_p)
#
low = guess
high = guess
#
bingo = pygame.mixer.Sound("bingo.mp3")
bingo.play(0)
#
elif guess > answer:
#
bg = tkinter.Canvas(root, width=1000, height=750)
bg.pack()
bg_p = tkinter.PhotoImage(file="too_high.png")
bg.create_image(500, 375, image=bg_p)
#
high = guess
#
lol = pygame.mixer.Sound("lol.mp3")
lol.play(0)
#
else:
#
bg = tkinter.Canvas(root, width=1000, height=750)
bg.pack()
bg_p = tkinter.PhotoImage(file="too_low.png")
bg.create_image(500, 375, image=bg_p)
#
low = guess
#
lol = pygame.mixer.Sound("lol.mp3")
lol.play(0)
#
while True:
#
for event in pygame.event.get():
#
if event.type == pygame.QUIT:
#
pygame.quit()
exit()
#
if event.type == pygame.MOUSEBUTTONDOWN:
#
onclick()
#
try:
#
l1.destroy()
l2.destroy()
#
except:
#
pass
#
finally:
#
l1 = tkinter.Label(root, text=str(low), font=("Arial", 60), bg="white")
l1.place(x=280, y=570)
l2 = tkinter.Label(root, text=str(high), font=("Arial", 60), bg="white")
l2.place(x=630, y=570)
#
root.mainloop()
After executing the program, _tkinter.TclError: can’t invoke “label” command: application has been destroyed is returned.
No matter where I put mainloop() it doesn’t work.
Why?
I put mainloop() on everywhere, but it still doesn’t work.
New contributor
Zelxa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.