Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What did I do wrongly?
#1
Sad 
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()
Gribouillis write Aug-07-2022, 08:11 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python reading variable in another py file wrongly _vertig0 2 1,955 Nov-21-2020, 07:19 AM
Last Post: _vertig0

Forum Jump:

User Panel Messages

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