Tkinter画布无法删除项目

时间:2016-04-28 12:00:58

标签: python tkinter tkinter-canvas

我一直在修补一个python问题,我正在使用tkinter从tkinter应用canvas.delete(args)制作图纸,没有任何反应。我使用print语句帮助调试项目ID看起来与那些相同用于创建项目请协助。 这是我的代码的一部分

lst = []
lst1 = []
lst2 = []
class A1:

    def __init__(self, master):
        self.canvas_width = 700
        self.canvas_height = 400
        master.title("A1 - StudentUPI") 
        geometry_string = str(self.canvas_width)+"x"+str(self.canvas_height)+"+10+20"
        master.geometry(geometry_string)
        self.a_canvas = Canvas(root)
        self.a_canvas.config(background="SlateBlue1")   
        self.a_canvas.pack(fill=BOTH, expand = True)
        self.create_button()
        self.coord = RandRegion.RandRegion(self.canvas_width,self.canvas_height)

    def create_button(self):
        self.frame = Frame(root, bg='grey', width=700, height=40)
        self.frame.pack(fill='x')
        self.button1 = Button(self.frame, text='Add Rect', command=self.randRect)
        self.button1.pack(side='left')
        self.button2 = Button(self.frame, text='Remove Rect', command=self.remRec)
        self.button2.pack(side='left')
        self.button3 = Button(self.frame, text='Add Circle', command=self.randCircle)
        self.button3.pack(side='left')
        self.button4 = Button(self.frame, text='Remove Circle', command=self.remCir)
        self.button4.pack(side='left')
        self.button5 = Button(self.frame, text='Add Arc', command=self.randArc)
        self.button5.pack(side='left')
        self.button6 = Button(self.frame, text='Remove Arc', command=self.remArc)
        self.button6.pack(side='left')
        self.button7 = Button(self.frame, text='Remove Red Shapes', command=self.remRed)
        self.button7.pack(side='left')

    def randRect(self):
        coord = RandRegion.RandRegion(self.canvas_width,self.canvas_height).get_coord()
        color = RandRegion.RandRegion(self.canvas_width,self.canvas_height).get_color()
        lst.append([coord,color])
        self.a_canvas.create_rectangle(coord,fill=color)
        print(coord)

    def randCircle(self):
        coord = RandRegion.RandRegion(self.canvas_width,self.canvas_height).get_coord()
        color = RandRegion.RandRegion(self.canvas_width,self.canvas_height).get_color()
        lst1.append([coord,color])
        self.a_canvas.create_oval(coord,fill=color)
        print(lst1)

    def randArc(self):
        coord = RandRegion.RandRegion(self.canvas_width,self.canvas_height).get_coord()
        color = RandRegion.RandRegion(self.canvas_width,self.canvas_height).get_color()
        lst2.append([coord,color])
        self.a_canvas.create_arc(coord, start=0, extent=90,fill=color)
        print(lst2)

    def remRec(self):
        self.a_canvas.delete(str(lst[random.randrange(len(lst))][0]))
        print((lst[random.randrange(len(lst))][0]))

    def remCir(self):
        self.a_canvas.delete(str(lst1[random.randrange(len(lst1))][0]))

    def remArc(self):
        self.a_canvas.delete(lst2[random.randrange(len(lst2))][0])

    def remRed(self):
        new_lis = lst 
        for x in range(len(new_lis)):
            if 'red' in (new_lis[x]):
                print(new_lis[x][0])
                self.a_canvas.delete(new_lis[x][0])


    print(lst,lst1,lst2)


root = Tk()
app = A1(root)
root.mainloop()
`

1 个答案:

答案 0 :(得分:1)

canvas.delete(item)项说明符(标记或ID)作为参数。因此,您应该将您的创作(a_canvas.create_xxx( ))分配给变量,然后使用这些变量删除它们。

Arcs的一个例子:

def randArc(self):
    ....
    arc = self.a_canvas.create_arc(coord, start=0, extent=90,fill=color)
    lst2.append(arc)

def remArc(self):
    # you need to remove that item from list as well
    removed_arc = lst2.pop(random.randrange(len(lst2))) 
    self.a_canvas.delete(removed_arc)