Python Forum
[Tkinter] Images in Toplevel()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Images in Toplevel()
#1
Hi All,

How do i add an image into a window in Tkinter when using toplevel. Ive tried with PIL and haven't got working results.
Reply
#2
Show what you tried (code).
Reply
#3
(Mar-08-2021, 02:18 PM)Larz60+ Wrote: Show what you tried (code).

Hi,

This is the whole program.

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)


        trophy=tk.Toplevel(window_4)
        trophy.geometry("500x400")
        trophy.title("Congratulations")

        trophy_canvas = tk.Canvas(window_4, height=500, width=400)
        trophy_image = ImageTk.PhotoImage(Image.open("D:\Work\School\ComputerScience\Code\Other\trophy.png"))
        image2 = trophy_canvas.create_image(0, 0, anchor=NW, image=trophy_image)
        trophy_canvas.grid(row=0, column=0)
        
    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()
And I get this error.

Error:
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> == RESTART: D:\Work\School\ComputerScience\Code\Other\Tkinter Game Testing.py == 9 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 240, in click_9 trophy_image = ImageTk.PhotoImage(Image.open("D:\Work\School\ComputerScience\Code\Other\trophy.png")) File "D:\Work\School\ComputerScience\Python Files\lib\site-packages\pillow-8.1.1-py3.9-win-amd64.egg\PIL\Image.py", line 2904, in open fp = builtins.open(filename, "rb") OSError: [Errno 22] Invalid argument: 'D:\\Work\\School\\ComputerScience\\Code\\Other\trophy.png' >>>
Reply
#4
Toplevel works the same as the window returned by Tk().

I cannot run your program because it references several files I don't have. I don't want to change every reference (far more than are needed) just so I can test. Your code is 322 lines long! You could make a working program that demonstrates your problem (placing image in a Toplevel window) in a program under 20 lines long. If you want help, you have to do a little work.

I chopped your program down to 12 lines and the source of the problem was immediately obvious. I suggest you take this opportunity to prune your 322 line program into a more appropriately sized example to post. While doing this I am sure you will see the problem too.

You might also consider testing new language features in small programs before including them in your main work. It is usually easier and faster to write a short test program than add code to a large program. Debugging and testing is certainly much faster. Once you have a working example it will be easier for you to integrate the feature with your existing code. Not only do you have a working example to compare against, but you also have a better understanding of how the feature works. A side benefit is you also have a short example you can post if you run into problems.
Reply
#5
(Mar-08-2021, 04:39 PM)deanhystad Wrote: Toplevel works the same as the window returned by Tk().

I cannot run your program because it references several files I don't have. I don't want to change every reference (far more than are needed) just so I can test. Your code is 322 lines long! You could make a working program that demonstrates your problem (placing image in a Toplevel window) in a program under 20 lines long. If you want help, you have to do a little work.

I chopped your program down to 12 lines and the source of the problem was immediately obvious. I suggest you take this opportunity to prune your 322 line program into a more appropriately sized example to post. While doing this I am sure you will see the problem too.

You might also consider testing new language features in small programs before including them in your main work. It is usually easier and faster to write a short test program than add code to a large program. Debugging and testing is certainly much faster. Once you have a working example it will be easier for you to integrate the feature with your existing code. Not only do you have a working example to compare against, but you also have a better understanding of how the feature works. A side benefit is you also have a short example you can post if you run into problems.

This code here is part of another program that will be later implemented into.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,138 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,831 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Toplevel window menator01 5 2,985 Apr-18-2022, 06:01 PM
Last Post: menator01
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 3,839 Jan-07-2022, 10:10 PM
Last Post: finndude
  Create image on a Toplevel using tkinter ViktorWong 3 7,745 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets iconit 2 2,399 Apr-28-2020, 06:50 AM
Last Post: iconit
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 2,642 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa
  [Tkinter] Toplevel window and global widgets? KevinBrown 3 5,113 Apr-25-2019, 06:02 PM
Last Post: Yoriz
  [Tkinter] Notebook accessing a specific tab using a button from a toplevel frame honestie 8 18,580 Sep-12-2018, 01:51 AM
Last Post: honestie
  [Tkinter] Close Toplevel after clicking button jollydragon 3 10,597 Jul-12-2018, 06:22 AM
Last Post: jollydragon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020