tkmotor.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # -*- Python -*-
4 
5 from Tkinter import *
6 import thread
7 import time
8 import math
9 
10 class TkMotor(Frame):
11  def __init__(self, num, radius, master=None):
12  Frame.__init__(self, master)
13  self.num = num
14  self.r = radius
15  self.init()
16  self.pack()
17 
18 
19 
20  def init(self):
21  self.space = 4
22  canwidth = self.r * self.space * self.num
23  canheight = self.r * self.space
24  self.can = Canvas(self, bg="white",
25  width=canwidth, height=canheight)
26  self.can.pack()
27  self.circ = []
28  self.line = []
29  self.x = []
30  self.y = []
31  self.x1 = []
32  self.y1 = []
33  self.x2 = []
34  self.y2 = []
35  self.sx1 = []
36  self.sy1 = []
37  self.sx2 = []
38  self.sy2 = []
39 
40  for i in xrange(self.num):
41  self.x.append(self.r * self.space / 2 * (i+0.5) * 2)
42  self.y.append(self.r * self.space / 2)
43  self.x1.append(self.x[i] - self.r)
44  self.y1.append(self.y[i] - self.r)
45  self.x2.append(self.x[i] + self.r)
46  self.y2.append(self.y[i] + self.r)
47  self.sx1.append(self.x[i] - self.r * 0.1)
48  self.sy1.append(self.y[i] - self.r * 0.1)
49  self.sx2.append(self.x[i] + self.r * 0.1)
50  self.sy2.append(self.y[i] + self.r * 0.1)
51  self.circ.append(self.can.create_oval(self.x1[i], self.y1[i],
52  self.x2[i], self.y2[i],
53  outline="#aaaaaa",
54  fill="#dddddd"))
55  self.circ.append(self.can.create_oval(self.sx1[i], self.sy1[i],
56  self.sx2[i], self.sy2[i],
57  outline="#000000",
58  fill="#000000"))
59  self.line.append(self.can.create_line(self.x[i], self.y[i],
60  self.x[i],
61  self.y[i] - self.r,
62  fill="#700040", width=5))
63 
64 
65  def set_angle(self, angle):
66  if len(angle) != self.num: return
67  i = 0
68  for a in angle:
69  a = a * math.pi / 180
70  self.can.delete(self.line[i])
71  self.line[i] = self.can.create_line(self.x[i], self.y[i],
72  self.x[i] + self.r * math.cos(a),
73  self.y[i] + self.r * math.sin(a),
74  fill="#700040", width=5)
75  i = i + 1
76 
77 
78 
79 def test ():
80  m = TkMotor(6, 40, Toplevel())
81  n = TkMotor(6, 40, Toplevel())
82  thread.start_new_thread(m.mainloop, ())
83  thread.start_new_thread(n.mainloop, ())
84  cnt = 0
85  while(1):
86  angle = [cnt % 360, - cnt % 360, cnt % 360, - cnt % 360, cnt % 360, - cnt % 360]
87  m.set_angle(angle)
88  time.sleep(0.001)
89  cnt = cnt + 1
90 
91 
92 if __name__ == '__main__': test()
def __init__(self, num, radius, master=None)
Definition: tkmotor.py:11


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:35