如何在 Tkinter 中添加图像? [英] How to add an image in Tkinter?

查看:86
本文介绍了如何在 Tkinter 中添加图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Tkinter 中添加图片?

How do I add an image in Tkinter?

这给了我一个语法错误:

This gave me a syntax error:

root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

推荐答案

上面的代码中没有语法错误"——它要么出现在其他行中(上面不是你的全部代码,因为没有导入,无论是 path 变量的声明)还是其他错误类型.

There is no "Syntax Error" in the code above - it either ocurred in some other line (the above is not all of your code, as there are no imports, neither the declaration of your path variable) or you got some other error type.

上面的例子对我来说效果很好,在交互式解释器上测试.

The example above worked fine for me, testing on the interactive interpreter.

这篇关于如何在 Tkinter 中添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆