/Demo/tkinter/matt/animation-w-velocity-ctrl.py

http://unladen-swallow.googlecode.com/ · Python · 44 lines · 26 code · 13 blank · 5 comment · 0 complexity · 5ae0aa0803f0f4648d1388e7836e71a1 MD5 · raw file

  1. from Tkinter import *
  2. # this is the same as simple-demo-1.py, but uses
  3. # subclassing.
  4. # note that there is no explicit call to start Tk.
  5. # Tkinter is smart enough to start the system if it's not already going.
  6. class Test(Frame):
  7. def printit(self):
  8. print "hi"
  9. def createWidgets(self):
  10. self.QUIT = Button(self, text='QUIT', foreground='red',
  11. command=self.quit)
  12. self.QUIT.pack(side=BOTTOM, fill=BOTH)
  13. self.draw = Canvas(self, width="5i", height="5i")
  14. self.speed = Scale(self, orient=HORIZONTAL, from_=-100, to=100)
  15. self.speed.pack(side=BOTTOM, fill=X)
  16. # all of these work..
  17. self.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="blue")
  18. self.draw.pack(side=LEFT)
  19. def moveThing(self, *args):
  20. velocity = self.speed.get()
  21. str = float(velocity) / 1000.0
  22. str = "%ri" % (str,)
  23. self.draw.move("thing", str, str)
  24. self.after(10, self.moveThing)
  25. def __init__(self, master=None):
  26. Frame.__init__(self, master)
  27. Pack.config(self)
  28. self.createWidgets()
  29. self.after(10, self.moveThing)
  30. test = Test()
  31. test.mainloop()