【问题标题】:Python tkinter erase lines/shapes from the canvas without erasing background imagePython tkinter 从画布上擦除线条/形状而不擦除背景图像
【发布时间】:2019-08-12 01:08:55
【问题描述】:

我正在做一个 python 项目,用户可以在其中以笔记本页面图像作为背景的画布上书写和绘图。 我想要的是用户可以使用鼠标移动擦除画布上的绘图。 我尝试使用 create_line 来绘制白色,但这仅适用于背景为白色的情况,但对于我的背景,它看起来就像背景也被删除了。

    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 但这也不起作用

【问题讨论】:

  • 我以前的做法是使用透明画布和背景图片。
  • 这个 ".c.delete(id(self.c.create_line))" 毫无意义。 为什么你使用函数.create_lineid来删除Canvas上的一个项目?
  • @StefvanderZon 你是如何使用背景图片的?.... ......我实际上是 tkinter 和 python 的新手
  • @AbhishekGhanekar 您可以将 pygame 表面视为层。一切都停留在屏幕上,直到其他东西被绘制在它上面。这意味着擦除某些东西的方法是在它上面画一些东西。我做了一个简单的绘图应用程序给你解释我之前说的。

标签: java python tkinter tkinter-canvas


【解决方案1】:

您无法使用画布以您想要的方式擦除。画布不是基于像素的绘画工具。您可以添加和删除对象,但不能仅覆盖或擦除对象的一部分。

【讨论】:

    【解决方案2】:

    我为您编写了一个小程序,以显示我之前在 cmets 中修复的内容。希望能帮助到你。

    您需要一个 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多