Tkinter Does Not Reflect Images

Asked 1 years ago, Updated 1 years ago, 331 views

What's troubling you

I'm trying to make software with python using Tkinter, but I can't see the image when I type the code.
Put the py file and the image file in the same folder and select the image file name.

Not only PhotoImage and Button, but also icons are not reflected, so the image file itself is probably impossible.

Tried

·Restart PC
·Uninstall and reinstall python
·Type python-V in cmd to confirm that the version appears
·Try with a different image
·Move the file to another location

* I'm a beginner, so I don't know how to solve it at all.If you are kind, please reply.

actual code

python tkinter

2022-12-22 21:30

1 Answers

The image was displayed successfully in my environment.
The active directory in the running environment may be different from the file path in the image.

Verify that the file is placed in the path shown by executing the code below.

importos
print(os.path.abspath('icon.ico'))# Full path to get icon

If the file is not located in the path shown, consider the following actions:

  • Place the file in the path that came out (recommended)
    C:\Users\Owner\OneDrive\Document\MyPython\Tkinter Course\icon.icoFile
    Go to C:\Users\Owner\OneDrive\Document\MyPython\icon.ico.
  • Move the working directory in os.chdir
    Add importos and os.chdir(r"C:\Users\Owner\OneDrive\Document\MyPython\Tkinter Course") around the third line of the code.
  • Change the working directory
    If you are using VS Code and running PowerShell from Terminal,
    Press Enter after PSC:\(omitted)\MyPython> in Terminal with cd C:\Users\Owner\OneDrive\Document\MyPython\Tkinter Course.
    Then enter the execution command for python.


2022-12-22 22:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.