This article mainly introduces how to use tkinter in python to move graphics with the mouse. It involves the implementation skills of Python Based on tkinter drawing and has some reference value, for more information about how to use tkinter in python, see the following example. We will share this with you for your reference. The details are as follows:
This is inspired by a js effect:
Two eyes move with the mouse
Running effect:
Code Section:
From tkinter import * #1. obtain the coordinates of the current circle (x1, y1) #2. obtain the Center Coordinate (x2, y2) of the circle moving from the incircle #3. move the circle from the coordinate (x1, y1) to the coordinate (x2, y2) _ author _ = {'name': 'hongten ', 'mail ': 'hongtenzone @ foxmail.com ', 'blog ':' http://blog.csdn.net/ ', 'Qq': '000000', 'created': '2017-09-20 '} class Eay (Frame): def createWidgets (self): # The playing field self. draw = Canvas (self, width = 500, height = 500) # cursor position self. mouse_x = 450 self. mouse_y = 250 # Center Coordinate (x, y) self. oval_zero_x = 250 self. oval_zero_y = 250 # self. oval_r = 100 # inner circle radius self. oval_R = 30 self. oval_r1 = self. oval_r-self. oval_R + 0.5 self. oval_r2 = self. oval_r-self. oval_R- 0.5 # small circle self. letter_ball_x1 = 250 self. letter_ball_y1 = 250 # self. ball = self. draw. create_oval (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 = "white") self. ball = self. draw. create_oval (self. oval_zero_x-self. oval_r1), (self. oval_zero_y-self. oval_r1), (self. oval_zero_x + self. oval_r1 ),( Self. oval_zero_y + self. 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") # Inner Circle self. ball_over = self. draw. create_oval (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_LOG: print ('#' * 50) print ('coordinates of the mouse :({},{})'. format (self. mouse_x, self. mouse_y) print ('current coordinate of the incircle is :({},{})'. format (self. letter_ball_x1, self. letter_ball_y1) ''' to obtain the circle coordinate (x2, y2) ''' ax_x = abs (self. mouse_x-self. oval_zero_x) ax_y = abs (self. mouse_y-self. oval_z Ero_y) if SHOW_LOG: print ('coordinate A (oval_zero_x, oval_zero_y) to coordinate X (mouse_x, mouse_y) the distance is AX ') print ('ax_x ={} in AX {}, ax_y = {}'. format (ax_x, ax_y) ax_len = (ax_x ** 2) + (ax_y ** 2) ** 0.5 if SHOW_LOG: print ('ax length: {}'. format (ax_len) # if the mouse coordinates are in (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 production :{}'. format (ac_len) if int (self. mous E_x-self. oval_zero_x )! = 0: if int (self. mouse_y-self. oval_zero_y )! = 0: # Calculate the straight line slope 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) ######################################## ########### coordinates after a circle is moved # three conditions are provided: #1. the circle center coordinates (x1, y1) of a small circle are on the AC (y = kx + B) #2. (r-R) ^ 2 = x1 ^ 2 + y1 ^ 2 can be obtained from 1, 2 => (r-R) ^ 2 = x1 ^ 2 + 2 * x1 * k * B + B ^ 2 => x1 has two values. Use 3 to judge the symbol of x1 and then obtain y1 # 3.if self. mousex_x> 0: # x1> 0 # This is a binary quadratic equation. There are two groups of solutions to the equation. However, you can use the cursor position self. mouse_x (self. mouse_y) to determine the coordinates of the center x1 (y1) letter_ball_x2 = (ac_len * (abs (self. mouse_x-self. oval_zero_x)/ax_len) + self. letter_ball_x1 letter_ball_y2 = (letter_ball_x2 * k) + B if SHOW_LOG: print ('current coordinate of a circle is :({},{})'. format (self. letter_ball_x1, self. letter_ball_y1) print ('coordinate after the circle is moved :({},{})'. format (letter_ball_x2, letter_ball_y2) # Move the circle from the coordinate (x1, y1) to the coordinate (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 :({},{})'. format (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 else: print ('Place the cursor over the X axis ') else: print ('Place the cursor over the Y axis') else: if SHOW_LOG: print ('coordinates after the circle is moved are the mouse coordinates ') # coordinate after the circle is moved letter_ball_x2 = self. mouse_x letter_ball_y2 = self. mouse_y if SHOW_LOG: print ('the coordinates after the circle is moved are :({},{})'. format (letter_ball_x2, letter_ball_y2) # Move the circle from the coordinate (x1, y1) to the coordinate (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 :({},{})'. format (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): # execute when you press the left button in the window and drag it # Widget. bind (self. draw ,"
", Self. mouseMove) # execute self when the mouse moves inside the circle. draw. tag_bind (self. ball, "", self. mouseMove) def _ init _ (self, master = None): global letter_ball_x2 letter_ball_x2 = 0 global letter_ball_y2 letter_ball_y2 = 0 global SHOW_LOG = True Frame. _ init _ (self, master) Pack. config (self) self. createWidgets () self. after (10, self. move_ball) game = Eay () game. mainloop ()
I hope this article will help you with Python programming.