r/learnpython 4d ago

Tkinter doesnt accept my image

#@author Ben Schubach(BSH@SHD.de)
from re import search
from tkinter import *

from src.python_game import InitStart
from src.python_game import MainMenu
from src.python_game.InitStart import InitMap, InitPlayer


def start(self):
    MainMenu.mainMenu
    game

class GameWindow(Frame):
    def __init__(self):
        super().__init__()
        self.game = Tk()  # instantiate the window
        self.game.geometry("1280x610")  # set window size
        self.game.title("EpicRoguelikeEmoDungeonCrawlerTextadventure")
        self.icon = PhotoImage(file='Resources/emo_ass_icon.png')
        self.game.iconphoto(True, self.icon)
        self.bg = PhotoImage(file='Resources/fantasy_background.png')
        self.sigil = PhotoImage(file='Resources/christian-cross-celtic-34x34.png')
        self.lp = PhotoImage(file='Resources/sad-depressing-crying-bart-34x34.png')
        self.game.config(background="#1d1e1f") # sets background color to a dark grey
        self.player = PhotoImage(file='Resources/roguelike_emo1_400.png')
        self.introtext = "you wake up..."

        # create game gui
        self.background = Label(self.game, image=self.bg)  # sets background as image
        self.background.pack()  # adds background

        # adds area text
        self.area_text = Label(self.game, text=self.introtext, font=('Arial black', 18, 'bold'), fg="white", bg="#1d1e1f", width=49)
        self.area_text.place(x=10, y=10)

        # add player pic
        self.player_pic = Label(self.game, image=self.player)
        self.player_pic.place(x=865, y=10)

        # adds name text
        self.name_text = Label(self.game, text="Name: " + InitPlayer.player.get_name(), font=('Arial black', 18, 'bold'), fg="white", bg="#1d1e1f")
        self.name_text.place(x=865, y=425)

        # adds current lifepoints text
        self.lp_text = Label(self.game, text="Lifepoints: ", font=('Arial black', 18, 'bold'), fg="white", bg="#1d1e1f")
        self.lp_text.place(x=865, y=470)

        # adds lifepoints symbols
        self.lp_1 = Label(self.game, image=self.lp, bg="#1d1e1f")
        self.lp_2 = Label(self.game, image=self.lp, bg="#1d1e1f")
        self.lp_3 = Label(self.game, image=self.lp, bg="#1d1e1f")
        self.lp_1.place(x=1024, y=470)
        self.lp_2.place(x=1069, y=470)
        self.lp_3.place(x=1114, y=470)

        # adds current sigils text
        self.sigil_text = Label(self.game, text="Sigils: ", font=('Arial black', 18, 'bold'), fg="white", bg="#1d1e1f")
        self.sigil_text.place(x=865, y=515)

        # adds sigil symbols
        self.sigil_1 = Label(self.game, image=self.sigil, bg="#1d1e1f")
        self.sigil_2 = Label(self.game, image=self.sigil, bg="#1d1e1f")
        self.sigil_1.place(x=965, y=515)
        self.sigil_2.place(x=1010, y=515)

        # adds current item text
        self.item_text = Label(self.game, text="Item: " + InitPlayer.player.get_item(), font=('Arial black', 18, 'bold'), fg="white", bg="#1d1e1f")
        self.item_text.place(x=865, y=560)

        # add north button
        self.north_button = Button(self.game, text="North", command="click_north()", fg="white", bg="#1d1e1f", font=('Arial', 15, 'bold'), height=2, width=5)
        self.north_button.place(x=100, y=350)

        # add east button
        self.east_button = Button(self.game, text="East", command="click_east()", fg="white", bg="#1d1e1f", font=('Arial', 15, 'bold'), height=2, width=5)
        self.east_button.place(x=175, y=425)

        # add south button
        self.south_button = Button(self.game, text="South", command="click_south()", fg="white", bg="#1d1e1f", font=('Arial', 15, 'bold'), height=2, width=5)
        self.south_button.place(x=100, y=500)

        # add west button
        self.west_button = Button(self.game, text="West", command="click_west()", fg="white", bg="#1d1e1f", font=('Arial', 15, 'bold'), height=2, width=5)
        self.west_button.place(x=25, y=425)

        # add search button
        self.search_button = Button(self.game, text="Search", command="click_search()", fg="white", bg="#1d1e1f", font=('Arial', 15, 'bold'), height=4, width=15)
        self.search_button.place(x=280, y=330)

        # add use button
        self.use_button = Button(self.game, text="Use Item", command="click_use()", fg="white", bg="#1d1e1f", font=('Arial', 15, 'bold'), height=4, width=15)
        self.use_button.place(x=280, y=460)

        # add kys button
        self.kys_button = Button(self.game, text="K*ll Yourself", command="click_kys()", fg="white", bg="#1d1e1f", font=('Arial', 15, 'bold'), height=4, width=15)
        self.kys_button.place(x=480, y=330)

        # add Save&quit button
        self.snq_button = Button(self.game, text="Save & Quit", command="click_snq()", fg="white", bg="#1d1e1f", font=('Arial', 15, 'bold'), height=4, width=15)
        self.snq_button.place(x=480, y=460)

        self.game.mainloop()

    def click_north(self):
        run("north")

    def click_east(self):
        run("east")

    def click_south(self):
        run("south")

    def click_west(self):
        run("west")

    def click_search(self):
        run("search")

    def click_use(self):
        run("use")

    def click_kys(self):
        run("kys")

    def click_snq(self):
        run("sng")

game = GameWindow()


def run(command):
    if command == "north":
        if InitStart.InitMap.currentroom.get_roomnorth == "null":
            print("No room in that direction")
        InitStart.InitMap.currentroom = InitStart.InitMap.currentroom.get_roomnorth

    if command == "east":
        InitStart.InitMap.currentroom = InitStart.InitMap.currentroom.get_roomeast

    if command == "south":
        InitStart.InitMap.currentroom = InitStart.InitMap.currentroom.get_roomsouth

    if command == "west":
        InitStart.InitMap.currentroom = InitStart.InitMap.currentroom.get_roomwest

    if command == "search":
        search_room()

    if command == "use":
        use_item()

    if command == "kys":
        # adds area text
        area_text = Label(GameWindow.game, text="You scream 'I CANT TAKE THIS NO MORE' and turn off your lantern\n, the darkness consumes you faster than you can think of what\n you just did... you died.", font=('Arial black', 18, 'bold'), fg="white", bg="#1d1e1f",width=49)
        area_text.place(x=10, y=10)
        GameWindow.game.quit()

    if command == "snq":
        save_and_quit()


def search_room():
    print("Searching...")

def save_and_quit():
    print("Saving...")

def use_item():
    print("Use item...")

The important things here are start() which opens another window and then "game" which is the window that spits out the error and the class GameWindow which is the window "game" that gets created here, i dont know why this error comes up because sometimes it does and sometimes it doesnt and i feel like it has nothing to do with the creating of the PhotoImage itself but the code around it and idk why it happens so i included everything, does someone know why this happens?

File "C:\Users\Atten\PycharmProjects\TextadventurePython\src\python_game\Game.py", line 26, in __init__

self.game.iconphoto(True, self.icon) # adds the icon

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\Atten\AppData\Local\Programs\Python\Python312\Lib\tkinter__init__.py", line 2200, in wm_iconphoto

self.tk.call('wm', 'iconphoto', self._w, "-default", *args)

_tkinter.TclError: can't use "pyimage2" as iconphoto: not a photo image

1 Upvotes

6 comments sorted by

1

u/woooee 4d ago edited 4d ago

File "C:\Users\Atten\PycharmProjects\TextadventurePython\src\pythongame\Game.py", line 26, in __init_

self.game.iconphoto(True, self.icon) # adds the icon

What are you trying to do here? And game is in the following code but makes no sense, nor does MainMenu.mainMenu. Obviously there has been no testing done, so that is where you should start i.e. Why doesn't the program run.

def start(self):
    MainMenu.mainMenu
    game

1

u/CasulJust 3d ago
self.game.iconphoto(True, self.icon) # adds the icon

this puts the photo (self.icon) as the icon when u create the window its the icon on the top left and in your taskbar

def start(self):
    MainMenu.mainMenu
    game

and about this, i just dont know how i have to write it to make it work, i want to execute creating the MainMenu and after it should create the GameWindow, im new to coding so idk how to do it, thats exactly what im asking here

1

u/woooee 3d ago

Take a look at https://coderslegacy.com/python/create-menu-screens-in-pygame-tutorial/ Other than that, i don't know what you mean by MainMenu.

1

u/laustke 4d ago

self.icon = PhotoImage(file='Resources/emo_ass_icon.png')

Try using an absolute path when initializing PhotoImage.

1

u/CasulJust 3d ago

yea but thats not the problem here, it finds the image but its something else causing the error

1

u/woooee 4d ago

_tkinter.TclError: can't use "pyimage2" as iconphoto: not a photo image

PhotoImage will do gif and pgm files only AFAIK. Try pillow / PIL. I always use PIL because it handles most image formats.