All you have to do to fix this is subtract x by half the width of the screen and multiply y by -1 and then add half the height of the screen to it. This picture shows that the Tkinter’s coordinates (red) and the turtle’s coordinates (blue) are different. self.x and self.y aren’t used alone in self.t.setposition(self.x-300, (self.y*-1)+300) because the origin of the turtle’s screen is in the middle while the event’s origin is on the top left corner of the screen. Take note that x and y are manipulated in the last line of run(). This program simply moves the turtle with the cursor. 'yPos evt. The statements: 'xPos evt.getXOnScreen () - getX () - 100 ' or. We need to add MouseMotionListener in order to get the X and Y coordinates of the cursor on the screen. (The “Motion” in screen.bind is surrounded by angle brackets) The timer will only start if Panel is dragged. This event argument contains the mouse coordinates.Īn example of func would be: def func(event): The function func accepts an event argument. To bind the mouse to the screen, do screen.bind('', func). The variable screen is the Tkinter object. Next, with this variable, make another variable: screen = wn.getcanvas(). Make a Screen object like this: wn = turtle.Screen(). Therefore, we can use Tkinter event binding to get the coordinates of the mouse on the screen.įirst, we need to retrieve the canvas from the screen. Turtle is a module that is based on Tkinter’s Canvas. In this tutorial I will show you how to do that.Ī turtle program where a line goes from the origin to the mouse Tkinter Binding You can also bind the keyboard’s keys to your program using this method. Getting the mouse coordinates of a turtle is useful if you want to make the turtle follow the mouse.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |