Java Python tkinter在不删除背景图像的情况下从画布中删除线条/形状
我正在做一个python项目,在这个项目中,用户可以在以笔记本页面图像为背景的画布上进行书写和绘制。 我想要的是,用户可以使用鼠标移动擦除画布上的图形。 我尝试使用create_line,它使用白色绘制,但这仅在背景为白色时有效,但对于我的背景,它看起来就像背景也被擦除一样Java Python tkinter在不删除背景图像的情况下从画布中删除线条/形状,java,python,tkinter,tkinter-canvas,Java,Python,Tkinter,Tkinter Canvas,我正在做一个python项目,在这个项目中,用户可以在以笔记本页面图像为背景的画布上进行书写和绘制。 我想要的是,用户可以使用鼠标移动擦除画布上的图形。 我尝试使用create_line,它使用白色绘制,但这仅在背景为白色时有效,但对于我的背景,它看起来就像背景也被擦除一样 def paint(self, event): self.line_width = self.choose_size_button.get() paint_color = self.color
def paint(self, event):
self.line_width = self.choose_size_button.get()
paint_color = self.color
if self.old_x and self.old_y:
self.c.create_line(self.old_x, self.old_y, event.x, event.y,
width=self.line_width, fill=paint_color,
capstyle=ROUND, smooth=TRUE,splinesteps=36,tags='rub')
if self.eraser_on :
self.c.delete(id(self.c.create_line))
self.old_x = event.x
self.old_y = event.y
def reset(self, event):
self.old_x, self.old_y = None, None
我在canvas.delete(event.x,event.y)中也使用了event.y event.y,但这不起作用您不能用画布以想要的方式擦除。画布不是基于像素的绘制工具。您可以添加和删除对象,但不能仅覆盖或擦除对象的一部分。您无法使用画布以所需的方式擦除对象。画布不是基于像素的绘制工具。您可以添加和删除对象,但不能仅覆盖或删除对象的一部分。我为您编写了一个小程序,以显示我在前面的注释中修复的内容。希望能有帮助 您需要一个640x480 test.png,它与程序位于同一文件夹中,您可以运行此代码。这只是一个简单的绘图应用程序 画布是要绘制的表面,屏幕对象是背景
import pygame as pg
from pygame import Color, Surface
WIDTH = 640
HEIGHT = 480
EMPTY = Color(0,0,0,0)
screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Drawing app")
bg = pg.image.load("test.png")
clock = pg.time.Clock()
#I create a transparant canvas
canvas = pg.Surface([640,480], pg.SRCALPHA, 32)
def main():
is_running = True
while is_running:
for event in pg.event.get():
if event.type == pg.QUIT:
is_running = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
is_running = False
elif event.type == pg.MOUSEMOTION:
if pg.mouse.get_pressed()[0]:
#if mouse 1 is pressed, you draw a circle on the location of the cursor
location = (pg.mouse.get_pos())
pg.draw.circle(canvas, (0,0,0), location, 20)
elif event.type == pg.MOUSEBUTTONDOWN:
#clear canvas on mouse button 3 press
if event.button == 3:
canvas.fill(EMPTY)
#I blit the background first!
screen.blit(bg,(0,0))
#afterwards I overwrite it with a transparant canvas with the drawing I want
screen.blit(canvas,(0,0))
pg.display.update()
clock.tick(200)
if __name__ == "__main__":
main()
我为你写了一个小程序来展示我在前面的评论中所做的改进。希望能有帮助 您需要一个640x480 test.png,它与程序位于同一文件夹中,您可以运行此代码。这只是一个简单的绘图应用程序 画布是要绘制的表面,屏幕对象是背景
import pygame as pg
from pygame import Color, Surface
WIDTH = 640
HEIGHT = 480
EMPTY = Color(0,0,0,0)
screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Drawing app")
bg = pg.image.load("test.png")
clock = pg.time.Clock()
#I create a transparant canvas
canvas = pg.Surface([640,480], pg.SRCALPHA, 32)
def main():
is_running = True
while is_running:
for event in pg.event.get():
if event.type == pg.QUIT:
is_running = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
is_running = False
elif event.type == pg.MOUSEMOTION:
if pg.mouse.get_pressed()[0]:
#if mouse 1 is pressed, you draw a circle on the location of the cursor
location = (pg.mouse.get_pos())
pg.draw.circle(canvas, (0,0,0), location, 20)
elif event.type == pg.MOUSEBUTTONDOWN:
#clear canvas on mouse button 3 press
if event.button == 3:
canvas.fill(EMPTY)
#I blit the background first!
screen.blit(bg,(0,0))
#afterwards I overwrite it with a transparant canvas with the drawing I want
screen.blit(canvas,(0,0))
pg.display.update()
clock.tick(200)
if __name__ == "__main__":
main()
我以前做的是使用透明画布和背景图像。这个“.c.delete(id(self.c.create_line))”没有任何意义。为什么要使用函数的
id
。创建行删除画布上的项目?。。。。我使用了photo=PhotoImage(file='image'),但我仍然无法理解如何从画布上删除这些项目……我对tkinter和python@AbhishekGhanekar您可以将pygame曲面视为层。在屏幕上绘制其他内容之前,所有内容都会一直显示在屏幕上。这意味着抹掉某样东西的方法就是在上面画上某样东西。我制作了一个简单的绘图应用程序来解释我之前所说的。我以前做的是使用透明画布和背景图像。这个“.c.delete(id(self.c.create_line))”没有任何意义。为什么要使用函数的id
。创建行删除画布上的项目?。。。。我使用了photo=PhotoImage(file='image'),但我仍然无法理解如何从画布上删除这些项目……我对tkinter和python@AbhishekGhanekar您可以将pygame曲面视为层。在屏幕上绘制其他内容之前,所有内容都会一直显示在屏幕上。这意味着抹掉某样东西的方法就是在上面画上某样东西。我做了一个简单的绘图应用程序给你解释我之前说的。