Python Forum
What did I do wrongly? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: What did I do wrongly? (/thread-37907.html)



What did I do wrongly? - Tomi - Aug-07-2022

It says icon.ico not defined, what should I do? The icon.ico is in the same folder as the script im trying to run.

from tkinter import *
from PIL import ImageTk, Image

root = Tk()

root.title('Images')

root.iconbitmap('icon.ico')

my_img = ImageTk.PhotoImage(Image.open("Pasi.png"))
my_label = Label(image=my_img)
my_label.pack()

button_quit = Button(root, text="Exit", command=root.quit)
button_quit.pack()

root.mainloop()



RE: What did I do wrongly? - Gribouillis - Aug-07-2022

(Aug-07-2022, 07:25 AM)Tomi Wrote: The icon.ico is in the same folder as the script im trying to run.
Python will use the path relative to the current working directory, not the directory containing the script. Two solutions:
  1. Hard code the full path to the file, such as "/spam/eggs/icon.ico"
  2. Have the code compute the full path like in the below example
from pathlib import Path
this_dir = Path(__file__).resolve().parent
icon_ico = str(this_dir / 'icon.ico')
If you want to distribute the program as a Python module, another option is to use package data files.