Python Forum

Full Version: What did I do wrongly?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
(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.