Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import PhotoImage, Canvas, Tk, NW, NE, X
- from PIL import Image
- #from point import Point
- class DrawingArea():
- def __init__(self, window):
- self.canvas_width = 920
- self.canvas_height = 640
- self.canvas = Canvas(window, width=self.canvas_width, height=self.canvas_height, border=2, relief="sunken")
- self.canvas.pack(side="left", fill="both", expand=True)
- # dodanie tla do canvas
- self.image = PhotoImage(file="img/image1.png")
- self.canvas.create_image(self.canvas_width, 0, anchor='ne', image=self.image)
- # do przechowywania wszystkich obrazkow
- self.images = []
- # do przechowywania ID obrazkow na canvas
- self.images_id = []
- #self.checkered(40)
- self.b1 = "UP"
- self.xold, self.yold = None, None
- self.canvas.bind('<Button-1>', self.pressed) # lewy button
- def checkered(self, line_distance):
- # vertical lines at an interval of "line_distance" pixel
- for x in range(line_distance, self.canvas_width, line_distance):
- self.canvas.create_line(x, 0, x, self.canvas_height, fill="#476042")
- # horizontal lines at an interval of "line_distance" pixel
- for y in range(line_distance, self.canvas_height, line_distance):
- self.canvas.create_line(0, y, self.canvas_width, y, fill="#476042")
- def pressed(self, event):
- self.b1 = "DOWN" # you only want to draw when the button is down
- x = int(event.x) - ( int(event.x) % 40 )
- y = int(event.y) - ( int(event.y) % 40 )
- #self.canvas.create_rectangle(x,y, x+40,y+40, fill="blue")
- # wczytanie i zapamietanie na liscie
- img = PhotoImage(file="img/tile.png")
- self.images.append(img)
- # dodanie do canvas
- img_id = self.canvas.create_image(x+20, y+20, image=img)
- # zapamietanie ID obrazka
- self.images.append(img_id)
- # dodanie prawego przycisku do obrazka
- self.canvas.tag_bind(img_id, '<Button-3>', lambda event:self.delete_image(event, img_id)) # prawy button
- print("x/y/id: %i %i %d" % (x, y, img_id) ) # because "Motion" events happen -all the time-
- def released(self, event):
- self.b1 = "UP"
- self.xold = None # reset the line when you let go of the button
- self.yold = None
- def delete_image(self, event, img_id):
- print("RIGHT BUTTON")
- print("delete_image event:", event, event.x, event.y)
- print("delete_image img_id:", img_id)
- # usuniecie z canvas
- self.canvas.delete(img_id)
- # szukanie na liscie ID
- if img_id in self.images_id:
- index = self.images_id.index(img_id)
- # usuniecie z listy ID
- del self.images_id[index]
- # usuniecie z listy obrazkow
- del self.images[index]
- if __name__=="__main__":
- window = Tk()
- app = DrawingArea(window)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement