如何将鼠标偏移 window 的角?

How do I offset the mouse by the window's corner?

在我正在制作的项目中,window 内有一个点跟随您的鼠标。问题是点是相对于显示器左上角的鼠标,而不是 window 的左上角。这使光标远离。这是我的代码的样子。

import pygame
import tkinter
root = tkinter.Tk()
pygame.init()
screen = pygame.display.set_mode((480, 360))
dot_image = pygame.image.load("dot.png").convert_alpha()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            break
    x = root.winfo_pointerx() - root.winfo_rootx()
    y = root.winfo_pointery() - root.winfo_rooty()
    screen.fill([0, 64, 64])
    screen.blit(dot_image, (x, y))
    pygame.display.update()
    pygame.time.Clock().tick(60)

我试过用 root.winfo_rooty() 减去 root.winfo_pointery() 或用它加法,但都没有任何效果。我搜索了如何让鼠标相对于 window,并得到了这个,但它对我不起作用。不过它确实沿着屏幕的左上角到达发球台!

不要混用不同的框架。在pygame中,鼠标的当前位置可以通过pygame.mouse.get_pos(). The return value is a tuple that represents the x and y coordinates of the mouse cursor. pygame.mouse.get_pressed() returns 表示状态的布尔值列表(TrueFalse ) 所有鼠标按钮。

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
clock = pygame.time.Clock()

dot_image = pygame.image.load("dot.png").convert_alpha()

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
    x, y = pygame.mouse.get_pos()
    screen.fill([0, 64, 64])
    screen.blit(dot_image, (x, y))
    pygame.display.update()
    clock.tick(60)

pygame.quit()