Python Forum

Full Version: PyImage4
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi ALl,

I have the below code, I keep getting this error message and am not sure why or what to change. The code and the error is below.

import tkinter as tk
from tkinter import *
import tkinter.font as font
import random
import time
from PIL import ImageTk,Image
from playsound import playsound
 
 
window_4 = tk.Tk()
 
window_4.title("Game")
 
window_4.attributes("-fullscreen", True)
 
random_num = random.randint(1, 9)
random_num = 9
print(random_num)
player_choice = 0
global lives
lives = 3
 
def click_1():
 
    global lives
    player_choice=1
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_1 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_1.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
 
def click_2():
 
    global lives
    player_choice=2
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_2 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_2.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
 
def click_3():
 
    global lives
    player_choice=3
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_3 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_3.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
 
def click_4():
 
    global lives
    player_choice=4
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_4 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_4.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
 
def click_5():
 
    global lives
    player_choice=5
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_5 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_5.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
             
def click_6():
 
    global lives
    player_choice=6
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_6 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_6.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
 
def click_7():
 
    global lives
    player_choice=7
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_7 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_7.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
 
def click_8():
 
    global lives
    player_choice=8
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_8 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_8.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
             
def click_9():
 
    global lives
    player_choice = 9
    if player_choice == random_num:
        label_instructions.grid_forget()
        playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\CorrectAnswer.mp3")
        correct_label = tk.Label(text="Correct Guess - Well Done. The game will now terminate. Thank You Playing, We Hope To See You Again Soon!!!!")
        correct_label.grid(row=0, column=2)
        window_4.after(3000, window_4.destroy)
        window_5 = tk.Tk()
        window_5.title("Congratulations")
        window_5.attributes("-fullscreen", True)
        trophy = Canvas(window_5, width = 100, height = 100)  
        trophy.grid(row=0, column=0)
        img_4 = ImageTk.PhotoImage(Image.open("D:\Work\School\ComputerScience\Code\Other\Trophy.png"))  
        trophy.create_image(20, 20, anchor=NW, image=img_4) 
    else:
        lives = lives - 1
        if lives == 2:
            heart_3.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 1:
            heart_2 .grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
        elif lives == 0:
            heart_1.grid_forget()
            playsound("D:\Work\School\ComputerScience\Code\Other\TkinterSounds\WrongAnswer.mp3")
            gameover_9 = tk.Label(text="GAME OVER, you have run out of your 3 lives! - Thanks for playing")
            gameover_9.grid(row=7, column=2)
            window_4.after(3000, window_4.destroy)
     
label_instructions = tk.Label(text="The computer has chosen a number, your job is to correctly guess the number. You only have 3 tries, Good Luck!")
label_instructions.grid(row=0, column = 2)

myFont = font.Font(size=82)
 
game_button_1 = tk.Button(text="1", height=1, width=7, command=click_1)
game_button_1['font'] = myFont
game_button_1.grid(row=1, column = 1)
 
game_button_2 = tk.Button(text="2", height=1, width=7, command=click_2)
game_button_2['font'] = myFont
game_button_2.grid(row=1, column = 2)
 
game_button_3 = tk.Button(text="3", height=1, width=7, command=click_3)
game_button_3['font'] = myFont
game_button_3.grid(row=1, column = 3)
 
clear_label_1 = tk.Label(text=" ")
clear_label_1.grid(row=2)
 
game_button_4 = tk.Button(text="4", height=1, width=7, command=click_4)
game_button_4['font'] = myFont
game_button_4.grid(row=3, column = 1)
 
game_button_5 = tk.Button(text="5", height=1, width=7, command=click_5)
game_button_5['font'] = myFont
game_button_5.grid(row=3, column = 2)
 
game_button_6 = tk.Button(text="6", height=1, width=7, command=click_6)
game_button_6['font'] = myFont
game_button_6.grid(row=3, column = 3)
 
clear_label_2 = tk.Label(text=" ")
clear_label_2.grid(row=4)
 
game_button_7 = tk.Button(text="7", height=1, width=7, command=click_7)
game_button_7['font'] = myFont
game_button_7.grid(row=5, column = 1)
 
game_button_8 = tk.Button(text="8", height=1, width=7, command=click_8)
game_button_8['font'] = myFont
game_button_8.grid(row=5, column = 2)
 
game_button_9 = tk.Button(text="9", height=1, width=7, command=click_9)
game_button_9['font'] = myFont
game_button_9.grid(row=5, column = 3)
 
 
heart_1 = Canvas(window_4, width = 300, height = 300)  
heart_1.grid(row=7, column=1)
img_1 = ImageTk.PhotoImage(Image.open("D:\Work\School\ComputerScience\Code\Other\pic_1.png"))  
heart_1.create_image(20, 20, anchor=NW, image=img_1) 
 
heart_2 = Canvas(window_4, width = 300, height = 300)  
heart_2.grid(row=7, column=2)
img_2 = ImageTk.PhotoImage(Image.open("D:\Work\School\ComputerScience\Code\Other\pic_2.png"))  
heart_2.create_image(20, 20, anchor=NW, image=img_2)
 
heart_3 = Canvas(window_4, width = 300, height = 300)  
heart_3.grid(row=7, column=3)
img_3 = ImageTk.PhotoImage(Image.open("D:\Work\School\ComputerScience\Code\Other\pic_3.png"))  
heart_3.create_image(20, 20, anchor=NW, image=img_3) 
 
window_4.mainloop()
Error:
Exception in Tkinter callback Traceback (most recent call last): File "D:\Work\School\ComputerScience\Python Files\lib\tkinter\__init__.py", line 1892, in __call__ return self.func(*args) File "D:\Work\School\ComputerScience\Code\Other\Tkinter Game Testing.py", line 239, in click_9 trophy.create_image(20, 20, anchor=NW, image=img_4) File "D:\Work\School\ComputerScience\Python Files\lib\tkinter\__init__.py", line 2790, in create_image return self._create('image', args, kw) File "D:\Work\School\ComputerScience\Python Files\lib\tkinter\__init__.py", line 2776, in _create return self.tk.getint(self.tk.call( _tkinter.TclError: image "pyimage4" doesn't exist
Tk() is called to initialize tkinter in your program, It can only be called once. If you want another top level window use the Toplevel widget. Though a dialog window may be a better choice for displaying a trophy.