2016-04-28 88 views
0

我一直在修補一個python問題,我使用tkinter在tkinter上應用canvas.delete(args)時使用圖畫,沒有任何反應。我使用print語句來幫助調試項目id看起來與創建物品時使用的相同,請協助。 這裏是我的代碼部分Tkinter畫布無法刪除項目

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

canvas.delete(item)需要項目符(標籤或ID)作爲參數。所以你應該把你的作品(a_canvas.create_xxx())分配給一個變量,然後用這些變量刪除它們。

一個例子爲您弧:

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) 
+0

謝謝,我終於想通了這一點 – dmn8