In this paper, the Tkinter of Python development is introduced to realize the method of moving the graph with mouse. Share to everyone for your reference, as follows:
When you do this, the inspiration comes from a JS effect:
Two eyes move with mouse movement
Operating effect:
Code section:
From tkinter Import * #1. Gets the current center coordinate of the small circle (x1, y1) #2. Gets the center coordinate (x2, y2) #3 to the small circle movement. Move the small circle from coordinates (x1, y1) to coordinates (x2, y2) __author__ = {' Name ': ' Hongten ', ' Mail ': ' hongtenzone@foxmail.com ', ' blog ': ' http://blog.csdn.net/', ' QQ ': ' 648719819 ', ' Created ': ' 2013-09-20 '}class Eay (Frame): def createwidgets (self): # # The playing field Self.draw = Canvas (self, width=500, height=500) #鼠标位置 self.mouse_x = self.mouse_y = #圆心坐标 (x, y) self.oval_zero_x = sel F.oval_zero_y = #外面大圆半径 Self.oval_r = #里面小圆半径 Self.oval_r = SELF.OVAL_R1 = Self.oval_r-self.ov Al_r + 0.5 self.oval_r2 = self.oval_r-self.oval_r-0.5 #小圆 self.letter_ball_x1 = Self.letter_ball_y1 = The ball is outside the great circle Self.ball = Self.draw.create_oval ((self.oval_zero_x-self.oval_r), (Self.ov Al_zero_y-self.oval_r), (self.oval_zero_x + Self.oval_r), (self.oval_zero_y + SE Lf.oval_r), Fill= "White") Self.ball = Self.draw.create_oval ((SELF.OVAL_ZERO_X-SELF.OVAL_R1), (Self.ova L_ZERO_Y-SELF.OVAL_R1), (self.oval_zero_x + self.oval_r1), (self.oval_zero_y + S ELF.OVAL_R1), fill= "Blue") Self.ball = Self.draw.create_oval ((SELF.OVAL_ZERO_X-SELF.OVAL_R2), (SELF.OVAL_ZERO_Y-SELF.OVAL_R2), (self.oval_zero_x + self.oval_r2), (Self.oval_zero_y + self.oval_r2), fill= "white") #里面小圆 Self.ball_over = Self.draw.create_ov Al ((Self.oval_zero_x-self.oval_r), (Self.oval_zero_y-self.oval_r), (self . oval_zero_x + Self.oval_r), (self.oval_zero_y + self.oval_r), fill= "Red") Self.draw.pack (Side=left) def mouseMove (Self, event): self.mouse_x = event.x self.mouse_y = Event.y if Show_lo G:print (' # ' * 50) Print (' mouse coordinates: ({}, {}) '. Format (self.mouse_x, self.mouse_y)) print (' small circle current coordinates are: ({}, {}) '. Format (self.letter_ball_x1, SELF.LETTER_BALL_Y1) "Gets to the center coordinate of the small circle movement (x2, y2)" ax_x = ABS (self.mouse_x-self.oval_zero_x) ax_y = ABS (Self.mou se_y-self.oval_zero_y) if Show_log:print (' coordinates a (oval_zero_x, oval_zero_y) to coordinate x (mouse_x, mouse_y) distance of Ax ') Prin T (' ax ax_x = {}, ax_y = {} '. Format (ax_x, ax_y)) Ax_len = ((ax_x * * 2) + (AX_Y * 2)) **0.5 if Show_log:print (' A The length of X is: {} '. Format (Ax_len)) #如果鼠标坐标在 (Ax_len > |r-r|) If Ax_len > Abs (self.oval_r-self.oval_r): Ac_len = ABS (self.oval_r-self.oval_r) if Show_log:print (' AC's yield: {} '. Format (Ac_len)) if int (self.mouse_x-self.oval_zero_x)! = 0:if Int (self.mouse_y-self.oval_zer o_y)! = 0: #求直线斜率 y = kx + b k = (self.mouse_y-self.oval_zero_y)/(self.mouse_x-self.oval_zero_x) If Show_log:print (' the slope of the line from the mouse to the center of the circle is: {} '. Format (k)) B = self.mOuse_y-(k * self.mouse_x) ################################################### #小圆移动后的坐标 #这里有三 Conditions: # 1. The center coordinate of the small circle (x1, y1) on the line ac (y = kx + b) # 2. (r-r) ^2 = x1^2 + y1^2 by 1, 2 can get = = (r-r) ^2 = x1^2 + 2*x1*k*b + b^2 + x1 has two values, through 3 to determine the X1 symbol, thus finding Y1 # 3.if SEL f.mousex_x > 0: # x1 > 0 #这是一个二元二次方程, the solution of the equation has two groups, but the position of the mouse self.mouse_x (self.mouse_y) can be judged by the center coordinates x1 (y1) letter_ball_x2 = ((Ac_len * (ABS (self.mouse_x-self.oval_zero_x)))/ax_len) + self.letter_ball_x1 Lette R_ball_y2 = (LETTER_BALL_X2 * k) + b if show_log:print (' small circle current coordinates are: ({}, {}) '. Format (self.letter_ball_x1, self.letter_ball_y1) print (' small circle moved after coordinates: ({}, {}) '. Format (letter_ball_x2, letter_ball_y2)) #把小圆从坐标 (x1, y 1) move to coordinates (x2, y2) self.moved_x2 = letter_ball_x2-self.letter_ball_x1 Self.moved_y2 = Letter_ball_y2-se lf.letter_ball_y1 if Show_log:print (' The distance to be moved is: ({}, {}) '. foRmat (int (self.moved_x2), int (self.moved_y2))) Self.draw.move (self.ball_over, int (self.moved_x2), int (self.moved_y 2)) self.letter_ball_x1 = letter_ball_x2 self.letter_ball_y1 = Letter_ball_y2 Else:prin T (' Mouse on X-axis ') else:print (' Mouse on y-axis ') else:if show_log:print (' The coordinates of the small circle's movement are the mouse coordinates ') #小圆移动后的坐标 letter_ball_x2 = self.mouse_x Letter_ball_y2 = self.mouse_y if Show_log:print (' small circle is moved after coordinates: ({}, {}) '. fo Rmat (letter_ball_x2, Letter_ball_y2)) #把小圆从坐标 (x1, y1) move to coordinates (x2, y2) self.moved_x2 = Letter_ball_x2-self.letter_ Ball_x1 self.moved_y2 = letter_ball_y2-self.letter_ball_y1 if Show_log:print (' The distance to be moved is: ({}, {}) '. forma T (int (SELF.MOVED_X2), int (self.moved_y2))) Self.draw.move (self.ball_over, int (self.moved_x2), int (self.moved_y2)) self.letter_ball_x1 = letter_ball_x2 self.letter_ball_y1 = Letter_ball_y2 def move_ball (self, *args): #当鼠标在窗口中按 The #Widget is executed when you drag the left button. Bind (Self.draw, "
", Self.mousemove) #当鼠标在大圆内移动的时候执行 self.draw.tag_bind (Self.ball," ", Self.mousemove) def __init__ (self, maste R=none): global letter_ball_x2 letter_ball_x2 = 0 Global Letter_ball_y2 letter_ball_y2 = 0 Global Show_log Show_log = True frame.__init__ (self, Master) Pack.config (self) self.createwidgets () Self.after (Self.mov E_ball) game = Eay () game.mainloop ()
I hope this article is helpful for Python program design.