Advertisement
here2share

# Tk_delete_obj.py

Oct 18th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. # Tk_delete_obj.py
  2.  
  3. from Tkinter import *
  4. import random
  5. import time
  6.  
  7. root = Tk()
  8. root.resizable(0,0)
  9. root.wm_attributes("-topmost", 1)
  10.  
  11. canvas = Canvas(root, width=500, height=400, bd=0, highlightthickness=0)
  12. canvas.pack()
  13.  
  14. class Ball:
  15.     def __init__(self, canvas, color):
  16.         self.canvas = canvas
  17.         self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
  18.         self.canvas.move(self.id, 245, 100)
  19.  
  20.         self.canvas.bind("<Button-1>", self.canvas_onclick)
  21.         self.text_id = self.canvas.create_text(300, 200, anchor='se')
  22.         self.canvas.itemconfig(self.text_id, text='Click Anywhere On Canvas')
  23.  
  24.     def canvas_onclick(self, event):
  25.         x,y = event.x, event.y
  26.         self.canvas.itemconfig(
  27.             self.text_id,
  28.             text="You clicked at ({}, {})".format(x, y)
  29.         )
  30.         self.canvas.delete(self.id)
  31.         self.id = canvas.create_oval(x, y, x+15, y+15, fill="red")
  32.  
  33.     def draw(self):
  34.         self.canvas.move(self.id, 0, -1)
  35.         self.canvas.after(50, self.draw)
  36.  
  37. ball = Ball(canvas, "red")
  38. ball.draw()
  39. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement