Home > Back-end >  Is there a way to fill a tk.Canvas Arc with gradient color instead of solid color?
Is there a way to fill a tk.Canvas Arc with gradient color instead of solid color?

Time:02-05

The following scripts creates a tk.Canvas widget will a filled arc. The filled color is solid, i.e uniformly filled. I would like a graded appearance in either the radial or angular direction. Is there a way to do this? If so, how can it be done?

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root)
canvas.grid(row=0, column=0, sticky="nsew", pady=10)
canvas.create_arc(10, 10, 200, 200, style=tk.PIESLICE, start=0, extent=359, fill="yellow", outline="blue", width=10)
root.mainloop()

Current Appearance: Current Appearance

Radial gradient: Radial gradient

Angular gradient: Angular gradient

CodePudding user response:

I don't think create_arc accepts gradients as fill values. I think your best bet is to create an image and display it in your window.

Creating radial gradients is pretty straightforward with drawSvg. With drawSvg it is also easy to draw lines around and in your arc.

For conic/angular gradient, it isn't as simple. I found how to draw one in enter image description here

Edit: refactoring code and adding colors as kwarg (now working for conic gradient too). Also if you use Windows, you'll have to install libcairo separately: install uniconvertor and make sure libcairo-2.dll is in your PATH.

  • Related