Python Tkinter如何获得画布的图像?

2024-10-03 06:30:17 发布

您现在位置:Python中文网/ 问答频道 /正文

正如标题所说,我需要得到画布的图像

要获得标签的图像,我只需键入

aLable = Label(root,image = AnImage)
aLabel.cget("image")

我可以使用cget()检查AmImage是否在aLabel中,那么如何对Canvas?{}进行同样的检查,似乎不起作用


Tags: 图像image标题键入画布root标签label
1条回答
网友
1楼 · 发布于 2024-10-03 06:30:17

我找到了一个方法,所以我会尽力解释

from tkinter import *
from PIL import ImageTk,Image

root = Tk()
An_Image = ImageTk.PhotoImage(Image.open('flower.jpg'))
Test = Canvas(root, width = 1000, height = 1000)
Test.create_image(25,25, image=An_Image, anchor=CENTER, tag = "abc")
Test.place(x=10,y = 10)

标记是小部件某些部分(如画布)的名称。 "abc"是已创建图像的标记的名称,可以随意命名,标记不能包含空格,并且必须是字符串tag = "your_string"

Image example

我们需要向项目添加标记的原因如下

Test.itemcget(Tag_id, Item) # Tag_id is the Item tag

#example 
Test.itemcget("abc", "image") # this is how I get the image from the Test Canvas

    

相关问题 更多 >