-1

All the methods i found on the internet can only provide me with the position on my desktop. Indeed i can set the canvas's position and calculate the position, but if the window is moved to another position, then the calculation will be wrong, cuz there's no methods to get the position of the window in turtle graphics

  • It was not so clear what the question was. But I assumed it was the last "there's no methods to get the position of the window in turtle graphics". So assume it was okay to use another lib to get it. If you edit the question I can upvote it so its more clear. Also the title there you talk about mouse movements. – starking Mar 26 '23 at 12:24
  • https://stackoverflow.com/questions/35732851/turtle-how-to-get-mouse-cursor-position-in-window – Сергей Кох Mar 26 '23 at 21:13

2 Answers2

0

If what you want is an event each time the cursor moves, then we can do something like:

from turtle import Screen, Turtle

def onmove(self, fun, add=None):
    if fun is None:
        self.cv.unbind('<Motion>')
    else:
        def eventfun(event):
            fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)

        self.cv.bind('<Motion>', eventfun, add)

def show_cursor_position(x, y):
    onmove(screen, None)  # disable handler inside handler

    turtle.clear()
    turtle.write((x, y), align='center', font=('Arial', 18, 'normal'))

    onmove(screen, show_cursor_position)  # reenable handler

screen = Screen()

turtle = Turtle()
turtle.hideturtle()

onmove(screen, show_cursor_position)

screen.mainloop()

As soon as you start moving the cursor in the window, its position will be displayed in the center of the window. onmove() is written in the form of a method that could be added to turtle like the screen's onclick() method.

cdlane
  • 40,441
  • 5
  • 32
  • 81
-1

Many ways of doing it I suggest this one:

You need to know the name of the task you can find it in "Task Manager". I used that window in my exampel.

from win32gui import FindWindow, GetWindowRect

window_handle = FindWindow(None, "Task Manager")
window_rect   = GetWindowRect(window_handle)

print(window_rect)

Here is the output when running it my termianl: (3461, 350, 4529, 1179)

I had issue install the lib first but it worked using: pip install pywin32

starking
  • 153
  • 10