python开发_tkinter_图形随鼠标移动
做这个东西的时候,灵感源自于一个js效果:
两个眼睛随鼠标移动而移动
运行效果:
===============================================
代码部分:
===============================================
1 from tkinter import * 2 3 #1.获取到小圆当前的圆心坐标(x1, y1) 4 #2.获取到小圆移动的圆心坐标(x2, y2) 5 #3.把小圆从坐标(x1, y1)移动到坐标(x2, y2) 6 7 __author__ = {'name' : 'Hongten', 8 'mail' : 'hongtenzone@foxmail.com', 9 'blog' : 'http://www.cnblogs.com/', 10 'QQ': '648719819', 11 'created' : '2013-09-20'} 12 13 class Eay(Frame): 14 15 def createWidgets(self): 16 ## The playing field 17 self.draw = Canvas(self, width=500, height=500) 18 19 #鼠标位置 20 self.mouse_x = 450 21 self.mouse_y = 250 22 23 #圆心坐标(x,y) 24 self.oval_zero_x = 250 25 self.oval_zero_y = 250 26 #外面大圆半径 27 self.oval_r = 100 28 29 #里面小圆半径 30 self.oval_R = 30 31 32 self.oval_r1 = self.oval_r - self.oval_R + 0.5 33 self.oval_r2 = self.oval_r - self.oval_R - 0.5 34 35 36 #小圆 37 self.letter_ball_x1 = 250 38 self.letter_ball_y1 = 250 39 40 # The ball 外面大圆 41 self.ball = self.draw.create_oval((self.oval_zero_x - self.oval_r), 42 (self.oval_zero_y - self.oval_r), 43 (self.oval_zero_x + self.oval_r), 44 (self.oval_zero_y + self.oval_r), 45 fill="white") 46 47 self.ball = self.draw.create_oval((self.oval_zero_x - self.oval_r1), 48 (self.oval_zero_y - self.oval_r1), 49 (self.oval_zero_x + self.oval_r1), 50 (self.oval_zero_y + self.oval_r1), 51 fill="blue") 52 53 self.ball = self.draw.create_oval((self.oval_zero_x - self.oval_r2), 54 (self.oval_zero_y - self.oval_r2), 55 (self.oval_zero_x + self.oval_r2), 56 (self.oval_zero_y + self.oval_r2), 57 fill="white") 58 59 #里面小圆 60 self.ball_over = self.draw.create_oval((self.oval_zero_x - self.oval_R), 61 (self.oval_zero_y - self.oval_R), 62 (self.oval_zero_x + self.oval_R), 63 (self.oval_zero_y + self.oval_R), 64 fill="red") 65 66 self.draw.pack(side=LEFT) 67 68 def mouseMove(self, event): 69 self.mouse_x = event.x 70 self.mouse_y = event.y 71 72 if SHOW_LOG: 73 print('#' * 50) 74 print('鼠标的坐标为:({}, {})'.format(self.mouse_x, self.mouse_y)) 75 print('小圆当前坐标为:({}, {})'.format(self.letter_ball_x1, self.letter_ball_y1)) 76 '''获取到小圆移动的圆心坐标(x2, y2)''' 77 ax_x = abs(self.mouse_x - self.oval_zero_x) 78 ax_y = abs(self.mouse_y - self.oval_zero_y) 79 if SHOW_LOG: 80 print('坐标A(oval_zero_x, oval_zero_y)到坐标X(mouse_x, mouse_y)的距离为AX') 81 print('AX中ax_x = {}, ax_y = {}'.format(ax_x, ax_y)) 82 ax_len = ((ax_x ** 2) + (ax_y ** 2))**0.5 83 if SHOW_LOG: 84 print('AX的长度为:{}'.format(ax_len)) 85 86 #如果鼠标坐标在(ax_len > |r-R|) 87 if ax_len > abs(self.oval_r - self.oval_R): 88 ac_len = abs(self.oval_r - self.oval_R) 89 if SHOW_LOG: 90 print('AC的产度为:{}'.format(ac_len)) 91 92 if int(self.mouse_x - self.oval_zero_x) != 0: 93 if int(self.mouse_y - self.oval_zero_y) != 0: 94 #求直线斜率 y = kx + b 95 k = (self.mouse_y - self.oval_zero_y)/(self.mouse_x - self.oval_zero_x) 96 if SHOW_LOG: 97 print('鼠标到大圆圆心的直线的斜率为:{}'.format(k)) 98 b = self.mouse_y - (k * self.mouse_x) 99 ################################################### 100 101 #小圆移动后的坐标 102 #这里有三个条件: 103 # 1.小圆的圆心坐标(x1, y1)在直线AC上(y = kx + b) 104 # 2.(r-R)^2 = x1^2 + y1^2 由1,2可以得到 => (r-R)^2 = x1^2 + 2*x1*k*b + b^2 => x1有两个值,通过3判断x1的符号,从而求出y1 105 # 3.if self.mousex_x > 0: 106 # x1 > 0 107 #这是一个二元二次方程,方程的解有两组,不过通过鼠标的位置self.mouse_x(self.mouse_y)可以判断圆心坐标x1(y1) 108 letter_ball_x2 = ((ac_len * (abs(self.mouse_x - self.oval_zero_x)))/ax_len) + self.letter_ball_x1 109 letter_ball_y2 = (letter_ball_x2 * k) + b 110 if SHOW_LOG: 111 print('小圆当前坐标为:({}, {})'.format(self.letter_ball_x1, self.letter_ball_y1)) 112 print('小圆移动后坐标为:({}, {})'.format(letter_ball_x2, letter_ball_y2)) 113 114 #把小圆从坐标(x1, y1)移动到坐标(x2, y2) 115 self.moved_x2 = letter_ball_x2 - self.letter_ball_x1 116 self.moved_y2 = letter_ball_y2 - self.letter_ball_y1 117 if SHOW_LOG: 118 print('需要移动的距离是:({}, {})'.format(int(self.moved_x2), int(self.moved_y2))) 119 self.draw.move(self.ball_over, int(self.moved_x2), int(self.moved_y2)) 120 self.letter_ball_x1 = letter_ball_x2 121 self.letter_ball_y1 = letter_ball_y2 122 else: 123 print('鼠标在X轴上') 124 else: 125 print('鼠标在Y轴上') 126 else: 127 if SHOW_LOG: 128 print('小圆的移动后的坐标就是鼠标坐标') 129 #小圆移动后的坐标 130 letter_ball_x2 = self.mouse_x 131 letter_ball_y2 = self.mouse_y 132 if SHOW_LOG: 133 print('小圆移动后坐标为:({}, {})'.format(letter_ball_x2, letter_ball_y2)) 134 135 #把小圆从坐标(x1, y1)移动到坐标(x2, y2) 136 self.moved_x2 = letter_ball_x2 - self.letter_ball_x1 137 self.moved_y2 = letter_ball_y2 - self.letter_ball_y1 138 if SHOW_LOG: 139 print('需要移动的距离是:({}, {})'.format(int(self.moved_x2), int(self.moved_y2))) 140 self.draw.move(self.ball_over, int(self.moved_x2), int(self.moved_y2)) 141 self.letter_ball_x1 = letter_ball_x2 142 self.letter_ball_y1 = letter_ball_y2 143 144 def move_ball(self, *args): 145 #当鼠标在窗口中按下左键拖动的时候执行 146 #Widget.bind(self.draw, "<B1-Motion>", self.mouseMove) 147 #当鼠标在大圆内移动的时候执行 148 self.draw.tag_bind(self.ball, "<Any-Enter>", self.mouseMove) 149 150 def __init__(self, master=None): 151 global letter_ball_x2 152 letter_ball_x2 = 0 153 global letter_ball_y2 154 letter_ball_y2 = 0 155 global SHOW_LOG 156 SHOW_LOG = True 157 158 Frame.__init__(self, master) 159 Pack.config(self) 160 self.createWidgets() 161 self.after(10, self.move_ball) 162 163 game = Eay() 164 165 game.mainloop()
========================================================
More reading,and english is important.
I'm Hongten
大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。
E | hongtenzone@foxmail.com B | http://www.cnblogs.com/hongten
========================================================