Advertisement
furas

Python - Tkinter - dodawanie i usuwanie obrazkow z Canvas

Jan 6th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. from tkinter import PhotoImage, Canvas, Tk, NW, NE, X
  2. from PIL import Image
  3. #from point import Point
  4.  
  5. class DrawingArea():
  6.    
  7.     def __init__(self, window):
  8.        
  9.         self.canvas_width = 920
  10.         self.canvas_height = 640
  11.  
  12.         self.canvas = Canvas(window, width=self.canvas_width, height=self.canvas_height, border=2, relief="sunken")
  13.         self.canvas.pack(side="left", fill="both", expand=True)
  14.  
  15.         # dodanie tla do canvas
  16.         self.image = PhotoImage(file="img/image1.png")
  17.         self.canvas.create_image(self.canvas_width, 0, anchor='ne', image=self.image)
  18.  
  19.         # do przechowywania wszystkich obrazkow
  20.         self.images = []
  21.  
  22.         # do przechowywania ID obrazkow na canvas
  23.         self.images_id = []
  24.        
  25.         #self.checkered(40)
  26.         self.b1 = "UP"
  27.         self.xold, self.yold = None, None
  28.        
  29.         self.canvas.bind('<Button-1>', self.pressed) # lewy button
  30.  
  31.     def checkered(self, line_distance):
  32.  
  33.         # vertical lines at an interval of "line_distance" pixel
  34.         for x in range(line_distance, self.canvas_width, line_distance):
  35.             self.canvas.create_line(x, 0, x, self.canvas_height, fill="#476042")
  36.  
  37.         # horizontal lines at an interval of "line_distance" pixel
  38.         for y in range(line_distance, self.canvas_height, line_distance):
  39.             self.canvas.create_line(0, y, self.canvas_width, y, fill="#476042")
  40.  
  41.     def pressed(self, event):              
  42.         self.b1 = "DOWN"           # you only want to draw when the button is down
  43.         x = int(event.x) - ( int(event.x) % 40 )
  44.         y = int(event.y) - ( int(event.y) % 40 )
  45.        
  46.         #self.canvas.create_rectangle(x,y, x+40,y+40, fill="blue")
  47.  
  48.         # wczytanie i zapamietanie na liscie
  49.         img = PhotoImage(file="img/tile.png")
  50.         self.images.append(img)
  51.  
  52.         # dodanie do canvas
  53.         img_id = self.canvas.create_image(x+20, y+20, image=img)
  54.  
  55.         # zapamietanie ID obrazka
  56.         self.images.append(img_id)
  57.  
  58.         # dodanie prawego przycisku do obrazka
  59.         self.canvas.tag_bind(img_id, '<Button-3>', lambda event:self.delete_image(event, img_id)) # prawy button
  60.        
  61.         print("x/y/id: %i %i %d" % (x, y, img_id) )      # because "Motion" events happen -all the time-
  62.  
  63.  
  64.     def released(self, event):
  65.         self.b1 = "UP"
  66.         self.xold = None           # reset the line when you let go of the button
  67.         self.yold = None
  68.  
  69.  
  70.     def delete_image(self, event, img_id):
  71.         print("RIGHT BUTTON")
  72.         print("delete_image event:", event, event.x, event.y)
  73.         print("delete_image img_id:", img_id)
  74.  
  75.         # usuniecie z canvas
  76.         self.canvas.delete(img_id)
  77.  
  78.         # szukanie na liscie ID
  79.         if img_id in self.images_id:
  80.             index = self.images_id.index(img_id)
  81.  
  82.             # usuniecie z listy ID
  83.             del self.images_id[index]
  84.             # usuniecie z listy obrazkow
  85.             del self.images[index]
  86.            
  87.        
  88. if __name__=="__main__":
  89.     window = Tk()
  90.     app = DrawingArea(window)
  91.     window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement