IDLE 中的自定义tkinter 图像

问题描述 投票:0回答:1

我想在

ctk
中的按钮上显示图像,但是我无法获取打开图像的代码。

这是有问题的部分:

img = ctk.CTkImage(Image.open("C:/Documents/a/cloud.png"))

我称之为:

label_weather = ctk.CTkButton(tabView.tab("Map"), text ="Currently cloudy",image=img,compound="left",command=label_weather)

label_weather.pack(padx=20, pady=20)

the image of the error

我将代码和图像放在同一个文件中,并尝试通过以不同的方式放置它来调用让它工作,例如:

img = ctk.CTkImage(Image.open("C:/a/cloud.png"))

img = ctk.CTkImage(Image.open("Documents/a/cloud.png"))

img = ctk.CTkImage(Image.open("a/cloud.png"))

img = ctk.CTkImage(Image.open("C:/a/cloud.png"))

我试图获取它的文件夹:

the folder where im trying to get it

python image python-idle customtkinter
1个回答
0
投票

代码中的错误是由于要打开的图像的路径造成的。

您上面尝试的所有路径都是错误的,因为 Documents 文件夹没有直接存储在 C:/ Documents 文件夹的完整路径是

C:/Users/[your username]/Documents

您应该尝试将代码中的路径替换为:

C:/Users/[your username]/Documents/a/cloud.png
(并将 [您的用户名] 替换为您在 Windows 中的用户名)。

© www.soinside.com 2019 - 2024. All rights reserved.