Jul-21-2020, 05:56 PM
(This post was last modified: Jul-21-2020, 05:56 PM by deanhystad.)
"self.cwd = os.getcwd" sets self.cwd equal to the function os.getcwd. When I add parens to evaluate the function I get a path that does not end with '/'. For example I get 'C:\Users\me\Musings' which does not result in a valid filename when I add 'image.png'. So this is an error:
from PIL import Image import os f = Image.open(os.getcwd() + 'Image.png')
Output:Traceback (most recent call last):
File "C:\Users\me\Musings\junk.py", line 3, in <module>
f = Image.open(os.getcwd() + 'Image.png')
File "C:\Program Files\Python38\lib\site-packages\PIL\Image.py", line 2809, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\me\\MusingsImage.png'
No tailing '\' in the cwd path, so no '\\' in the file it tries to open. But this program that uses pathlib.Path works fine.from pathlib import Path from PIL import Image f = Image.open(Path.cwd() / 'Image.png') f.show()However when I use the same idea in tkinter I get the same error you are seeing.
from tkinter import * from pathlib import Path root = Tk() image_file = Path.cwd() / 'image.png' print(image_file) open_button = Button(root, image=image_file)
Output:C:\Users\me\musings\image.png
Traceback (most recent call last):
File "C:\Users\me\musings\junk.py", line 7, in <module>
open_button = Button(root, image=image_file)
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 2645, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: image "C:\Users\me\musings\image.png" doesn't exist
This is when I finally realize I had blindly followed you down the rabit hole and the button image argument is expecting an image object, not a filename. Duh!!! Helps to read the documentation instead of banging away at the keyboard sometimes.from tkinter import * from pathlib import Path root = Tk() image_file = Path.cwd() / 'image.png' image = PhotoImage(file = image_file) open_button = Button(root, image=image)When debugging it is always a good idea to debug the real problem and not what we think is the problem. The first and often the hardest step.