简体   繁体   English

tkinter为什么不显示图像?

[英]Why doesn't tkinter show me images?

Each time I try to load an image onto my tkinter app, it refuses to load, rather it springs different errors. 每次我尝试将图像加载到我的tkinter应用程序时,它都会拒绝加载,但会弹出不同的错误。

I have used so many different versions of the code. 我使用了很多不同版本的代码。 Below is the latest, which still dosen't work but throws an error. 下面是最新的,仍然无效,但会引发错误。

from PIL import Image, ImageTk # I have added the import of ImageTk
import tkinter

window = tkinter.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
imageFile = '\User\PycharmProjects\BRIGHTBROWN\PyShop\tkinter\studentsRecord\myface.jpg'
im1 = ImageTk.PhotoImage(Image.open(imageFile))
panel = tkinter.Label(window, image = im1)
panel.pack(side = "bottom", fill = "both", expand = "yes")

window.mainloop()

The error thrown is: 引发的错误是:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \\UXXXXXXXX escape SyntaxError:(unicode错误)“ unicodeescape”编解码器无法解码位置0-1的字节:截断的\\ UXXXXXXXX转义

The problem is with the path. 问题出在路径上。
Make your you provide the complete path "C:\\...\\User\\..." . 让您提供完整的路径 "C:\\...\\User\\..." After that, there are several ways you can fix it: 之后,有几种方法可以修复它:

Method #1: 方法1:
Replace the backward slash with the forward one. 将反斜杠替换为正斜杠。 For example: "C:/.../User/..." 例如: "C:/.../User/..."

Method #2: 方法2:
Use r behind the string. 在字符串后面使用r For example: r"C:\\...\\User\\..." 例如: r"C:\\...\\User\\..."

Method #3: 方法3:
Use Double Backward slashes. 使用双反斜杠。 For example: "C:\\\\...\\\\User\\\\..." 例如: "C:\\\\...\\\\User\\\\..."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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