简体   繁体   中英

No file or Directory exist, but it does

I am trying to use tkinter module to open a window to show a picture and was previously having trouble with: _tkinter.TclError: couldn't recognize data in image I have since used the following this question to restructured my code to the following: How do I insert a JPEG image into a python Tkinter window? I have made a few changes to the code as I am using python 3. When I have tried to execute the code: I get an error saying no file or directory exist. However i have used the following code to check: python open() method IOError: [Errno 2] No such file or directory: Below are the result of checking the files:

 >>> os.listdir()
['comments.py', 'Conan.txt', 'conditions.py', 'dateandtime.py', 'desktop.ini', 'dictionaries.py', 'exceptions.py', 'forging_functions.py', 'formatting.py', 'graphics.py', 'Hello.py', 'leapdays.py', 'logging.py', 'loop.py', 'modules.py', 'months.py', 'numbers.py.py', 'Opening_files.py', 'Picture.jpg', 'print_hello.py.py', 'tkintercode1.py', 'tkintercode2.py', 'user_input.py', 'Writing_file.py', '__pycache__']
  >>> os.getcwd()
'C:\\Users\\Draco\\OneDrive\\Documents\\Programming'
 >>> os.chdir(r'C:\\Users\\Draco\\OneDrive\\Documents\\Programming')
 >>> open('Picture.jpg')
 <_io.TextIOWrapper name='Picture.jpg' mode='r' encoding='cp1252'>

Below is the code I am working with:

import tkinter as tk
from tkinter import *

window = tk.Tk()
window.title("Random Image")
window.geometry("300x300")
window.configure(background='grey')

path = "C:\Draco\OneDrive\Documents\Programming\Picture.jpg" 


img = tkinter.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)

panel.pack(side = "bottom", fill = "both", expand = "yes")

window.mainloop()

Many thanks for any help

"C:\\Draco\\OneDrive\\Documents\\Programming\\Picture.jpg"
try path ="C:\\\\Draco\\\\OneDrive\\\\Documents\\\\Programming\\\\Picture.jpg" - double slash
EDIT
You have diff in paths. Users\\Draco but in code just Draco

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM