[英]How to turn a tkinter canvas circle shape into an image
图片1:
图 2:
我想将 tkinter 画布形状变成图像。 我该怎么做?
Python代码
class Bricks:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(5, 5, 25, 25, fill=color, width=2)
查看该线程以从画布中保存图像。 只需制作一个较小的画布,绘制 jour 圆圈并保存它。
from tkinter import *
from PIL import ImageGrab
def getter(widget):
x=root.winfo_rootx()+widget.winfo_x()
y=root.winfo_rooty()+widget.winfo_y()
x1=x+widget.winfo_width()
y1=y+widget.winfo_height()
ImageGrab.grab().crop((x,y,x1,y1)).save("file path here")
root = Tk()
cv = Canvas(root)
cv.create_rectangle(10,10,50,50)
cv.pack()
root.mainloop()
请参阅PhotoImage从文件加载图像。
from tkinter import PhotoImage
PhotoImage(file="path to image file")
使用canvas.create_image(x_pos, y_pos, image=some photoimage)
来绘制它。
from tkinter import PhotoImage
image = PhotoImage(file="path to image file")
root = Tk()
cv = Canvas(root)
cv.create_image(10,10, image=image)
cv.pack()
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.