繁体   English   中英

如何将焦点更改为 pygame window?

[英]How to change focus to pygame window?

我正在尝试使用pygame制作游戏,最后会弹出tkinter消息,显示游戏结束消息和再次玩的选项。

当我按下“再次播放”按钮时,玩家精灵在按下键时根本不会移动。

然后我发现它正在发生,因为在我按下消息框按钮后,它没有将焦点更改为活动的pygame window。

如果有人能告诉我如何改变焦点 window,那将会很有帮助。

如果您使用 Windows ,您可以使用win32gui模块,尤其是SetFocus()方法。

例如, 受此页面启发,下面的程序创建了一个tkinter window 和一个pygame Z05B8C74CBD976FBF2DE4C1 并将它们带到 frontFF42

# -------------------------------
# initialise the front() function

import win32gui

def windowEnumerationHandler(hwnd, windows):
    windows.append((hwnd, win32gui.GetWindowText(hwnd)))

results = []
windows = []
win32gui.EnumWindows(windowEnumerationHandler, windows)

def front(win_name):
    for i in windows:
        if i[1] == win_name:
            win32gui.ShowWindow(i[0],5)
            win32gui.SetForegroundWindow(i[0])
            break

# ------------------------
# create the pygame window

import pygame

class PygameWindow:
    def __init__(self, name):
        self.name = name
        self.window = pygame.display.set_mode((640, 480))
        pygame.display.set_caption(self.name)

    def to_front(self):
        front(self.name)

pygame_window = PygameWindow('This is the pygame window')

# -------------------------
# create the tkinter window

import tkinter

class TkinterWindow:
    def __init__(self, name):
        self.name = name
        self.window = tkinter.Tk()
        self.window.title(self.name)

    def to_front(self):
        front(self.name)

tkinter_window = TkinterWindow('This is the tkinter window')

# ------------------------------------------
# main loop - switch between the two windows

from time import sleep
while True:
    sleep(1)
    pygame_window.to_front()
    sleep(1)
    tkinter_window.to_front()
    
    # update the tkinter window to make it to stay visible
    tkinter_window.window.update()

暂无
暂无

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

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