简体   繁体   English

为什么我的 tkinter window 上无法加载我的图像?

[英]Why can't my image load on my tkinter window?

I'm trying to add an image to my program but it's not working out, the code is correct as shown below, and the image i'm trying to open is in the same folder as the saved.py file.我正在尝试将图像添加到我的程序中,但无法正常工作,代码正确,如下所示,并且我尝试打开的图像与 save.py 文件位于同一文件夹中。

from tkinter import *
from PIL import ImageTk, Image

root = Tk()
root.title("Balance 0-21")
root.configure(width=400, height=200)
root.iconbitmap("C:/Users/user/Desktop/Projects/Balance 0-21/LogoCon.ico")

MasterCard = ImageTk.PhotoImage(Image.open("ten.png"))
MasterCardIMG = Label(image=MasterCard)
MasterCardIMG.grid(row=2, column=3)

root.mainloop()

try adding this after the MasterCardIMG Label creation (also for convention make the first letter lowercase as these aren't classes), I was having the same issue getting my images to show, I believe it has something to do with the label instance creation not being able to assign the image.尝试在创建 MasterCardIMG Label 之后添加它(也为了约定,将第一个字母小写,因为这些不是类),我在显示图像时遇到了同样的问题,我相信这与 label 实例创建无关能够分配图像。

MasterCardIMG.image = MasterCard

(recommendation for convention) (公约建议)

masterCard = ImageTk.PhotoImage(Image.open("ten.png"))
masterCardIMG = Label(image=masterCard)
masterCardIMG.image = masterCard
masterCardIMG.grid(row=2, column=3)

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

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