python 获取鼠标在图片上的坐标

2025-03-10 08:46:47
推荐回答(1个)
回答1:

下pygame module,然后


import pygame
pygame.init()
screen = pygame.display.set_mode([100,100]) ##size of window
your_image = pygame.image.load("your_image_name.png") ## image must be in the same folder, else path must be specified
while 1:
    screen.blit(your_image,[0,0]) ##pos of your image on the window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    
    position = pygame.mouse.get_pos() ##position of mouse on window
    print position
    pygame.display.set_caption(str(position)) ##make it the title of the window
    pygame.display.flip()