用图像填充 Tkinter 画布元素 [英] Filling a Tkinter Canvas Element with an Image
问题描述
有什么方法可以用图像填充 Tkinter 元素(更具体地说是椭圆形).如果没有,有没有办法调整图像大小以适应椭圆形?我也不想使用 PIL.
Is there any way to fill a Tkinter element (more specifically an oval) with an image. If not, is there any way to resize an image to fit into an oval? I also would prefer not to use PIL.
canvas.create_oval(val1, val2, val1+sz, val2+sz, fill=clr, outline=outln)
如何使图像适合这样的圆圈?
如果您想知道,我当然也会剔除图像周围的边缘.
How would you get an image to fit inside a circle like so?
I would also certainly cull the edges around the image if you were wondering.
推荐答案
在普通的 Tkinter 中,您的选择是有限的.您可以做的一件事是创建一个中间有一个透明圆圈的面具"图像.然后降低你的自己的形象在背后.(虽然效率不高)
In plain Tkinter your options are limited. One thing you could do is create a 'mask' image that has a transparent circle in the middle. Then lower your own image in behind it. (not very efficient though)
from tkinter import *
root = Tk()
canvas = Canvas(root, width=200, height=200, bd=0,
highlightthickness=0)
canvas.pack()
mask = PhotoImage(width=200, height=200)
cx,cy = 100,100 # image & circle center point
r = 100 # circle radius
r_squared = r*r
for y in range(200):
for x in range(200):
# using the formula for a circle:
# (x - h)^2 + (y - k)^2 = r^2
# any pixel outside our circle gets filled
if (x - cx)**2 + (y - cy)**2 > r_squared:
mask.put('blue', (x,y))
canvas.create_image(100,100, image=mask, anchor='c')
myimage = PhotoImage(file='bigpython.gif')
item = canvas.create_image(100,100, image=myimage, anchor='c')
canvas.lower(item)
root.mainloop()
这篇关于用图像填充 Tkinter 画布元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!