Advertisement
here2share

# Tk_mouse_right_delete_objects.py

Feb 21st, 2021
1,926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. # Tk_mouse_right_delete_objects.py
  2.  
  3. from Tkinter import *
  4. from PIL import ImageDraw, ImageTk, Image, ImageGrab
  5. from math import sin, cos, pi
  6. import random
  7.  
  8. root = Tk()
  9. root.title("Tk Mouse Right Delete Objects")
  10. ### root.withdraw() # vs root.deiconify()')
  11. xm,ym = 600,600
  12.  
  13. canvas = Canvas(root, width=xm, height=ym)
  14. canvas.grid()
  15.  
  16. xy = []
  17. def click(event):
  18.     x,y = event.x,event.y
  19.     print x,y
  20.     xy.append([x,y])
  21.    
  22. def delete_obj(event,obj):
  23.     print obj
  24.     canvas.delete(obj)
  25.  
  26. def rounded_button():
  27.     vertex_count = 100
  28.     t =  pi/vertex_count
  29.     a = 1.577
  30.     VERTEX = [(x1,y1+r)]
  31.     VERTEX += [(    x1+r*cos(i*2*t+a), \
  32.                     y1+r*sin(i*2*t+a)) for i in range(vertex_count/2)]
  33.     VERTEX += [(x2,y1-r)]
  34.     VERTEX += [[(   x2+r*cos(i*2*t+a), \
  35.                     y1+r*sin(i*2*t+a))] for i in range(vertex_count/2,vertex_count)]
  36.     font_style = ''
  37.     if r > 21+borderw:
  38.         font_style = 'bold italic'
  39.     fff = ' '.join(['ariel', str(int(max(6,r*0.6))), font_style])
  40.     obj = 'obj_'+str(ccc)
  41.     canvas.create_polygon(VERTEX, fill=fillc, width=borderw, outline=borderc, tag=obj)
  42.     canvas.create_text(x1+(x2-x1)/2, y1, text='Python!', font=fff, fill=borderc, tag=obj)
  43.     canvas.tag_bind(obj, '<Button-3>', lambda event, i=obj: delete_obj(event,i))
  44. root.bind('<Button-1>', click)
  45.  
  46. def pop2(zzz):
  47.     L = len(zzz)
  48.     return zzz[ccc%L]
  49. 0
  50. zzz = '''
  51. red orange green blue purple gray black
  52. red orange yellow green blue purple
  53. white
  54. black
  55. '''.strip().splitlines()
  56. btn_colors = [[(z,zzz[i+2]),(zzz[i+2],z)] for i in range(2) for z in zzz[i].split()]
  57. btn_colors = [[a,b] for z in btn_colors for a,b in z]
  58. random.shuffle(btn_colors)
  59. border_width = list(range(10))
  60. xya = [(x,y) for x in range(100,xm-200,50) for y in range(100,ym-200,50)]
  61. random.shuffle(xya)
  62. xyb = [(x,y) for x in range(0,100,5) for y in range(24,16*5,4)]
  63. random.shuffle(xyb)
  64.  
  65. sss = 'Click Mouse Right To Delete Target Objects'
  66. canvas.create_text(xm/2, 40, text=sss, font='ariel 21 italic', fill='blue')
  67.  
  68. ccc = 0
  69. while 1:
  70.     if xy:
  71.         fillc,borderc = pop2(btn_colors)
  72.         borderw = pop2(border_width)
  73.         x1,y1 = xy.pop()
  74.         x2,r = pop2(xyb)
  75.         x2 = x1+x2+r+20 ### to fit around text
  76.         rounded_button()
  77.         xy = []
  78.         ccc += 1
  79.     canvas.update()
  80. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement