Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用鼠标删除圆_Python_Python 3.x_User Interface_Tkinter - Fatal编程技术网

Python 用鼠标删除圆

Python 用鼠标删除圆,python,python-3.x,user-interface,tkinter,Python,Python 3.x,User Interface,Tkinter,我试图用鼠标右键删除鼠标创建的圆圈。我还想分别删除每个圆。我用这个答案()修改我的代码来创建圆圈,但是当我试图删除每个圆圈时,我总是得到一个错误。代码如下: import tkinter as tk from functools import partial class DrawCircles(tk.Canvas): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs

我试图用鼠标右键删除鼠标创建的圆圈。我还想分别删除每个圆。我用这个答案()修改我的代码来创建圆圈,但是当我试图删除每个圆圈时,我总是得到一个错误。代码如下:

import tkinter as tk 
from functools import partial

class DrawCircles(tk.Canvas):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        image = self.create_rectangle(0, 0, 300, 300, width=4, fill='grey')
        self.tag_bind(image, '<Button-1>', self.on_click)
        self.tag_bind(image, '<Button1-Motion>', self.on_motion)
        self.tag_bind(image, '<Button-3>', self.delete)

    def delete(self):
        super().delete('all')

    def on_click(self, event):
        self.start = event.x, event.y
        self.current = self.create_oval(*self.start, *self.start, width=5,outline='yellow')
        self.tag_bind(self.current, '<Button-1>', partial(self.on_click_circle, self.current))
        self.tag_bind(self.current, '<Button1-Motion>', self.on_motion)


    def on_click_circle(self, tag, event):
        self.current = tag
        x1, y1, x2, y2 = self.coords(tag)
        if abs(event.x-x1) < abs(event.x-x2):
            x1, x2 = x2, x1
        if abs(event.y-y1) < abs(event.y-y2):
            y1, y2 = y2, y1
        self.start = x1, y1
        print(x1,y1,x2,y2)

    def on_motion(self, event):
        self.coords(self.current, *self.start, event.x, event.y)

    def on_move(self,event):
        component = self.current
        x1, y1, x2, y2 = self.coords(component)
        locx, locy = component.winfo_x(), component.winfo_y()
        mx ,my =component.winfo_width(),component.winfo_height()
        xpos=(locx+event.x)-int(mx/2)
        ypos=(locy+event.y)-int(my/2)
    
        if xpos>=0 and ypos>=0: 
            component.place(x=xpos,y=ypos)


if __name__ == '__main__':
    main = DrawCircles()
    main.pack()
    main.mainloop()
将tkinter作为tk导入
从functools导入部分
类DrawCircles(tk.Canvas):
定义初始化(自,主=无,**kwargs):
super().\uuuu init\uuuu(主控,**kwargs)
image=self。创建_矩形(0,0,300,300,宽度=4,填充为灰色)
self.tag_绑定(图像,,,self.on_单击)
self.tag_绑定(图像,,,self.on_运动)
self.tag_绑定(图像,,,self.delete)
def删除(自我):
super().delete('all'))
单击时的def(自身,事件):
self.start=event.x,event.y
self.current=self.create_oval(*self.start,*self.start,width=5,outline='yellow')
self.tag\u bind(self.current',partial(self.on\u click\u circle,self.current))
self.tag_绑定(self.current',self.on_运动)
单击圆圈上的def(自身、标记、事件):
self.current=标记
x1,y1,x2,y2=自坐标(标记)
如果abs(事件x-x1)=0和YPO>=0:
位置(x=xpos,y=ypos)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main=DrawCircles()
main.pack()
main.mainloop()
错误:

delete()接受1个位置参数,但给出了2个

您的
delete()
函数在按下“Button-3”时被调用,也就是说,它作为画布上的事件被触发,因此它接受一个您没有传递的额外参数(如
e
event
或其他任何参数)

因此,在函数调用期间传递
e
,或者在函数定义期间将
e
定义为参数

self.tag_bind(image, '<Button-3>', lambda e: self.delete()) # Same as event or any_var_name_at_all

def delete(self,*args):
,检查?非常感谢@苏杰
def delete(self,event): # Same as e or any_var_name_at_all
    super().delete('all')