图像未显示在Tkinter GUI窗口中

时间:2020-03-04 00:28:33

标签: python image tkinter

基本上,我无法在tkinter GUI窗口中打开图像(.jpg)。

我遵循了本教程:https://www.youtube.com/watch?v=lt78_05hHSk

但是我只得到一个空白窗口。

from tkinter import *

root = Tk()

photo = PhotoImage(file=r"C:\Users\xxxx\OneDrive\Desktop\123k.jpg")
# photo = PhotoImage(file="123k.jpg")
label = Label(root, image=photo)
label.pack()

root.mainloop()

文件路径正确,并且.jpg文件与tkinter兼容。可能会发生什么?

我收到此错误

Traceback (most recent call last): 
File "C:\Users\xxx\ytrewq.py", line 5, 
in <module> photo = PhotoImage(file=r"C:\Users\xxxx\OneDrive\Desktop\123k.jpg") 
File "C:\Users\xxxx\AppData\Local\Programs\Thonny\lib\tkinter_init_.py", line 3545,
in init Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\xxxx\AppData\Local\Programs\Thonny\lib\tkinter_init_.py", line 3501, 
in init self.tk.call(('image', 'create', imgtype, name,) + options) 
_tkinter.TclError: couldn't recognize data in image file 
"C:\Users\xxxx\OneDrive\Desktop\123k.jpg"

0 个答案:

没有答案