Advertisement
here2share

# Tk_delete_objects.py

Sep 17th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. # Tk_delete_objects.py
  2.  
  3. from Tkinter import *
  4. import random
  5. root = Tk()
  6.  
  7. h = 360
  8. w = 540
  9.  
  10. objs = []
  11. colors='red orange yellow green cyan blue purple goldenrod khaki grey black'.split()
  12. c=colors[:]
  13. def removeobjs():
  14.     if len(objs) > 0:
  15.         canvas.delete(objs.pop())
  16.  
  17. def randomobjs():
  18.     w2 = (random.randrange(100))
  19.     h2 = (random.randrange(250))+10
  20.     w3 = (random.randrange(200))+180
  21.     h3 = (random.randrange(250))+10
  22.     if not c: [c.append(z) for z in colors]
  23.     objs.append(canvas.create_line(w2, h2, w3, h3, width=20, fill=c.pop()))
  24.  
  25.  
  26. canvas = Canvas(root)
  27. canvas.pack()
  28. frame = Frame(root, bg='grey', width=w, height=h)
  29. frame.pack(fill='x')
  30.  
  31. button1 = Button(frame, text='Add', command=randomobjs)
  32. button1.pack(side='left', padx=10)
  33. button2 = Button(frame, text='Remove', command=removeobjs)
  34. button2.pack(side='left')
  35.  
  36. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement