Advertisement
here2share

# Tk_thread_ani.py

May 21st, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. # Tk_thread_ani.py
  2.  
  3. from Tkinter import *
  4. import thread, time
  5.  
  6. trace = 0
  7.  
  8. howto = 'XY Drag on screen to create circles and rectangles\nPress o and r to move them as by shape'
  9. class CanvasEventsDemo:
  10.     def __init__(self, parent=None):
  11.         canvas = Canvas(width=600, height=600, bg='beige')
  12.         canvas.pack()
  13.         canvas.bind('<ButtonPress-1>', self.onStart)      # click
  14.         canvas.bind('<B1-Motion>',     self.onGrow)       # and drag
  15.         canvas.bind('<Double-1>',      self.onClear)      # delete all
  16.         canvas.bind('<ButtonPress-3>', self.onMove)       # move latest
  17.         self.canvas = canvas
  18.         self.drawn  = None
  19.         self.kinds = [canvas.create_oval, canvas.create_rectangle]
  20.     def onStart(self, event):
  21.         self.shape = self.kinds[0]
  22.         self.kinds = self.kinds[1:] + self.kinds[:1]      # start dragout
  23.         self.start = event
  24.         self.drawn = None
  25.     def onGrow(self, event):                              # delete and redraw
  26.         canvas = event.widget
  27.         if self.drawn: canvas.delete(self.drawn)
  28.         objectId = self.shape(self.start.x, self.start.y, event.x, event.y)
  29.         if trace: print objectId
  30.         self.drawn = objectId
  31.     def onClear(self, event):
  32.         event.widget.delete('all')                        # use tag all
  33.         self.canvas.create_text(300, 30, text=howto)
  34.     def onMove(self, event):
  35.         if self.drawn:                                    # move to click spot
  36.             if trace: print self.drawn
  37.             canvas = event.widget
  38.             diffX, diffY = (event.x - self.start.x), (event.y - self.start.y)
  39.             canvas.move(self.drawn, diffX, diffY)
  40.             self.start = event
  41.  
  42. class CanvasEventsDemoTags(CanvasEventsDemo):
  43.     def __init__(self, parent=None):
  44.         CanvasEventsDemo.__init__(self, parent)
  45.         self.canvas.create_text(300, 30, text=howto)
  46.         self.canvas.master.bind('<KeyPress-o>', self.onMoveOvals)    
  47.         self.canvas.master.bind('<KeyPress-r>', self.onMoveRectangles)  
  48.         self.kinds = self.create_oval_tagged, self.create_rectangle_tagged
  49.     def create_oval_tagged(self, x1, y1, x2, y2):
  50.         objectId = self.canvas.create_oval(x1, y1, x2, y2)
  51.         self.canvas.itemconfig(objectId, tag='ovals', fill='blue')
  52.         return objectId
  53.     def create_rectangle_tagged(self, x1, y1, x2, y2):
  54.         objectId = self.canvas.create_rectangle(x1, y1, x2, y2)
  55.         self.canvas.itemconfig(objectId, tag='rectangles', fill='red')
  56.         return objectId
  57.     def onMoveOvals(self, event):
  58.         print 'moving ovals'
  59.         self.moveInSquares(tag='ovals')           # move all tagged ovals
  60.     def onMoveRectangles(self, event):
  61.         print 'moving rectangles'
  62.         self.moveInSquares(tag='rectangles')
  63.     def moveInSquares(self, tag):                 # 5 reps of 4 times per sec
  64.         for i in range(5):
  65.             for (diffx, diffy) in [(+20, 0), (0, +20), (-20, 0), (0, -20)]:
  66.                 self.canvas.move(tag, diffx, diffy)
  67.                 self.canvas.update()              # force screen redraw/update
  68.                 time.sleep(0.25)                  # pause, but don't block gui
  69.  
  70.  
  71. class CanvasEventsDemoThread(CanvasEventsDemoTags):
  72.     def moveEm(self, tag):
  73.         for i in range(5):
  74.             for (diffx, diffy) in [(+20, 0), (0, +20), (-20, 0), (0, -20)]:
  75.                 self.canvas.move(tag, diffx, diffy)
  76.                 time.sleep(1.0)                      # pause this thread only
  77.     def moveInSquares(self, tag):
  78.         thread.start_new_thread(self.moveEm, (tag,))
  79.  
  80. if __name__ == '__main__':
  81.     CanvasEventsDemoThread()
  82.     mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement