繁体   English   中英

Python / Pygame鼠标位置没有更新,程序失去焦点

[英]Python/Pygame mouse position not updating and program losing focus

我正在尝试学习Python / Pygame。 我创建了一个可以使用鼠标位置的程序,但是当我从IDLE和命令提示符运行它时,鼠标位置都不会更新,当我点击图形窗口时它会进入非响应模式。

代码非常简单(见下文)。 会发生什么是print-command一遍又一遍地打印原始鼠标位置。 有任何想法吗?

import pygame
from pygame.locals import *
pygame.init()
Screen = pygame.display.set_mode([1000, 600])
MousePos = pygame.mouse.get_pos()
Contin = True
while Contin:
    print(MousePos)

您没有将MousePos更新为新值,而是反复打印相同的值。 你需要的是:

import pygame
from pygame.locals import *
pygame.init()
Screen = pygame.display.set_mode([1000, 600])
MousePos = pygame.mouse.get_pos()
Contin = True
while Contin:
    MousePos = pygame.mouse.get_pos()
    print(MousePos)
    DoSomething(MousePos)

注意:如果您不处理任何其他事件,这将进入非响应模式。

这是处理PyGame中事件的更好方法:

while running:
     event = pygame.event.poll()
     if event.type == pygame.QUIT:
         running = 0
     elif event.type == pygame.MOUSEMOTION:
         print "mouse at (%d, %d)" % event.pos

将while循环更改为:

while Contin:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Contin = False
    MousePos = pygame.mouse.get_pos()
    print(MousePos)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM