【问题标题】:Python Tkinter How to get the image of the Canvas?Python Tkinter 如何获取 Canvas 的图像?
【发布时间】:2021-01-11 18:49:55
【问题描述】:

正如标题所说,我需要获取画布的图像

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

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

我可以使用cget() 检查AmImage 是否在aLabel 中,那么我该如何处理Canvas? cget("image") 似乎不起作用

【问题讨论】:

  • 您想取回您添加到画布的单个图像,还是包含所有对象的整个画布的图像?对于单个图像,您可能应该在添加时保留对对象 ID 的引用。
  • 我需要从画布中获取单个图像
  • 研究画布的itemcget方法。
  • 你能给我一些链接吗?或者给我解释一下
  • 可以参考tk document

标签: python tkinter canvas


【解决方案1】:

我找到了一种方法,所以我会尝试解释它

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

我们需要给item加标签的原因是这个

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

    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 2021-07-18
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多