感谢this question,我发现了tksvg.

我已经知道如何显示SVG文件:

import tkinter as tk
import tksvg

window = tk.Tk()
svg_image = tksvg.SvgImage(file="tests/orb.svg")
label = tk.Label(image=svg_image)
label.pack()
window.mainloop()

以及如何使用SVG数据/字符串执行相同的操作:

import tkinter as tk
import tksvg

svg_string = """
<svg aria-hidden="true" focusable="false" role="img" viewBox="0 0 24 24" class="" fill="none" stroke-width="2" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><g stroke-width="1.5px" stroke="#B8B8B8" fill="none"><path stroke="none" d="M0 0h24v24H0z" fill="none" stroke-width="1.5px"></path><line x1="4" y1="20" x2="7" y2="20" stroke="#B8B8B8" fill="none" stroke-width="1.5px"></line><line x1="14" y1="20" x2="21" y2="20" stroke="#B8B8B8" fill="none" stroke-width="1.5px"></line><line x1="6.9" y1="15" x2="13.8" y2="15" stroke="#B8B8B8" fill="none" stroke-width="1.5px"></line><line x1="10.2" y1="6.3" x2="16" y2="20" stroke="#B8B8B8" fill="none" stroke-width="1.5px"></line><polyline points="5 20 11 4 13 4 20 20" stroke="#B8B8B8" fill="none" stroke-width="1.5px"></polyline></g></svg>
"""
window = tk.Tk()
svg_image = tksvg.SvgImage(data=svg_string)
label = tk.Label(image=svg_image)
label.pack()
window.mainloop()

但我想知道,如何修改SVG元素的Fill选项(基于SVG数据).

我想到的唯一方法就是重画/删除并使用Fill中使用的不同 colored颜色 创建SVG元素. 以下是我的try :

import tkinter as tk
import tksvg

svg_string = '<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="red"/></svg>'


def change_color(event):
    global svg_string
    fill_color = "blue" if "red" in svg_string else "red"
    svg_string = '<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="{}"/></svg>'.format(fill_color)
    canvas.delete(image_store[0])
    svg_store.pop(0)
    svg_store.append(tksvg.SvgImage(data=svg_string))
    image_store[0] = canvas.create_image(100, 100, image=svg_store[0])

window = tk.Tk()
canvas = tk.Canvas(window, width=200, height=200)
canvas.pack()

image_store = []
svg_store = []
svg_store.append(tksvg.SvgImage(data=svg_string))
image_store.append(canvas.create_image(100, 100, image=svg_store[0]))

canvas.tag_bind(image_store[0], "<Button-1>", change_color)
window.mainloop()

这里我试着来回切换 colored颜色 ,这样当它是蓝色的时候,它就变成了红色,当它是红色的时候,它变成了蓝色.但在这种情况下, colored颜色 似乎只会改变一次.

我只是想做一个解决办法,因为我认为这还不受支持(如果我错了,请随时纠正我,因为我试图查看代码,尽我最大的努力理解它).

我怎么能这样做呢?是使用上述解决办法(重画元素)还是使用更好的替代方案?

我使用的是Windows 10,Python版本3.8.10,Tkinter 8.6.9

推荐答案

除了"acw1668"的答案. 创建新的TkImage成本很高,您只需使用新数据更新旧映像即可.因此,使用image.configure(data=data_string)是有意义的,示例如下:

def change_color(event):
    global svg_string
    fill_color = "blue" if "red" in svg_string else "red"
    svg_string = '<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="{}"/></svg>'.format(fill_color)
    img, id_ = image_store[0], svg_store[1]
    img.configure(data=svg_string)
    canvas.itemconfig(id_, image=img)

Python相关问答推荐

如何随着收件箱的增加动态添加到HTML表的右下角?

Python 枕头上的图像背景变黑

计算每月过go x年的平均值

Python中的锁定类和线程以实现dict移动

根据多列和一些条件创建新列

运行回文查找器代码时发生错误:[类型错误:builtin_index_or_system对象不可订阅]

根据条件将新值添加到下面的行或下面新创建的行中

仿制药的类型铸造

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

运行Python脚本时,用作命令行参数的SON文本

如何将Docker内部运行的mariadb与主机上Docker外部运行的Python脚本连接起来

如何获取numpy数组的特定索引值?

PyQt5,如何使每个对象的 colored颜色 不同?'

组/群集按字符串中的子字符串或子字符串中的字符串轮询数据框

Scrapy和Great Expectations(great_expectations)—不合作

如果满足某些条件,则用另一个数据帧列中的值填充空数据帧或数组

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

如何从需要点击/切换的网页中提取表格?

Django admin Csrf令牌未设置

旋转多边形而不改变内部空间关系