tkmotor.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # -*- Python -*-
00004 
00005 from Tkinter import *
00006 import thread
00007 import time
00008 import math
00009 
00010 class TkMotor(Frame):
00011         def __init__(self, num, radius, master=None):
00012                 Frame.__init__(self, master)
00013                 self.num = num
00014                 self.r = radius
00015                 self.init()
00016                 self.pack()
00017 
00018 
00019 
00020         def init(self):
00021                 self.space = 4
00022                 canwidth = self.r * self.space * self.num
00023                 canheight = self.r * self.space
00024                 self.can = Canvas(self, bg="white",
00025                                                   width=canwidth, height=canheight)
00026                 self.can.pack()
00027                 self.circ = []
00028                 self.line = []
00029                 self.x = []
00030                 self.y = []
00031                 self.x1 = []
00032                 self.y1 = []
00033                 self.x2 = []
00034                 self.y2 = []
00035                 self.sx1 = []
00036                 self.sy1 = []
00037                 self.sx2 = []
00038                 self.sy2 = []
00039                 
00040                 for i in xrange(self.num):
00041                         self.x.append(self.r * self.space / 2 * (i+0.5) * 2)
00042                         self.y.append(self.r * self.space / 2)
00043                         self.x1.append(self.x[i] - self.r)
00044                         self.y1.append(self.y[i] - self.r)
00045                         self.x2.append(self.x[i] + self.r)
00046                         self.y2.append(self.y[i] + self.r)
00047                         self.sx1.append(self.x[i] - self.r * 0.1)
00048                         self.sy1.append(self.y[i] - self.r * 0.1)
00049                         self.sx2.append(self.x[i] + self.r * 0.1)
00050                         self.sy2.append(self.y[i] + self.r * 0.1)
00051                         self.circ.append(self.can.create_oval(self.x1[i], self.y1[i],
00052                                                                                                   self.x2[i], self.y2[i],
00053                                                                                                   outline="#aaaaaa",
00054                                                                                                   fill="#dddddd"))
00055                         self.circ.append(self.can.create_oval(self.sx1[i], self.sy1[i],
00056                                                                                                   self.sx2[i], self.sy2[i],
00057                                                                                                   outline="#000000",
00058                                                                                                   fill="#000000"))
00059                         self.line.append(self.can.create_line(self.x[i], self.y[i],
00060                                                                                                   self.x[i],
00061                                                                                                   self.y[i] - self.r,
00062                                                                                                   fill="#700040", width=5))
00063 
00064                 
00065         def set_angle(self, angle):
00066                 if len(angle) != self.num: return
00067                 i = 0
00068                 for a in angle:
00069                         a = a * math.pi / 180
00070                         self.can.delete(self.line[i])
00071                         self.line[i] = self.can.create_line(self.x[i], self.y[i],
00072                                                                  self.x[i] + self.r * math.cos(a),
00073                                                                  self.y[i] + self.r * math.sin(a),
00074                                                                   fill="#700040", width=5)
00075                         i = i + 1
00076 
00077 
00078 
00079 def test ():
00080         m = TkMotor(6, 40, Toplevel())
00081         n = TkMotor(6, 40, Toplevel())
00082         thread.start_new_thread(m.mainloop, ())
00083         thread.start_new_thread(n.mainloop, ())
00084         cnt = 0
00085         while(1):
00086                 angle = [cnt % 360, - cnt % 360, cnt % 360, - cnt % 360, cnt % 360, - cnt % 360]
00087                 m.set_angle(angle)
00088                 time.sleep(0.001)
00089                 cnt = cnt + 1
00090                 
00091 
00092 if  __name__ == '__main__': test()


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28