专栏首页Hongtenpython开发_tkinter_图形随鼠标移动

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()

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • python开发_thread_线程_搜索本地文件

    ========================================================

    Hongten
  • pygame系列_箭刺Elephant游戏_源码下载

    http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html

    Hongten
  • python开发_tkinter_小球完全弹性碰撞游戏_源码下载

    ===================================================

    Hongten
  • 详解文本分类之DeepCNN的理论与实践

    最近在梳理文本分类的各个神经网络算法,特地一个来总结下。下面目录中多通道卷积已经讲过了,下面是链接,没看的可以瞅瞅。我会一个一个的讲解各个算法的理论与实践。目录...

    zenRRan
  • IOS开发:一对一直播系统中视频录制切换前后摄像头后方向错误解决办法

    最近发现项目里,切换前后摄像头几次之后,会出现录制的视频方向上下颠倒,可是明明已经在切换摄像头后,给videoconnection重新设置了录制方向,我项目视频...

    布谷鸟网络科技
  • ios开发:关于一对一直播源码中视频录制切换前后摄像头后方向错误解决办法

    最近发现项目里,一对一直播源码切换前后摄像头几次之后,会出现录制的视频方向上下颠倒,可是明明已经在切换一对一直播系统摄像头后,给videoconnection重...

    布谷安妮
  • python pyqt5 pandas处理数据

    """ Module implementing MainWindow. """

    用户5760343
  • python pyqt5 按钮 QRadioButton 常用

    import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.Qt...

    用户5760343
  • python爬虫入门之爬万本书籍

    Python现在非常火,语法简单而且功能强大,很多同学都想学Python!所以小的给各位看官们准备了高价值Python学习视频教程及相关电子版书籍,都放在了文章...

    python学习教程
  • PyQt5 日历控件(QCalendarWidget)

    点击日期时会发射.clicked 信号,信号参数类型为PyQt5.QtCore.QDate

    用户6021899

扫码关注云+社区

领取腾讯云代金券