如何在 pygame 中检测可调整大小的窗口状态并在 Linux 中将其取消最大化? [英] How to detect resizeable window state in pygame and de-maximize it in Linux?

查看:115
本文介绍了如何在 pygame 中检测可调整大小的窗口状态并在 Linux 中将其取消最大化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 pygame 在 python 中构建的应用程序,它最初以不可调整的设置大小显示登录屏幕,然后当用户登录游戏时,保存的设置用于转换窗口大小.登录后窗口变为 RESIZABLE.如果用户注销,则窗口将更改回没有 RESIZABLE 标志的初始大小.只要用户从普通窗口注销,一切都好,但是当用户点击最大化按钮然后在某些发行版中注销时,窗口仍然保持最大化并且登录屏幕被绘制在窗口的左上角.

I have an application built in python with use of pygame that initially displays a login screen at a set size which is not RESIZABLE and then when user logs into the game the saved settings are being used to transform the window size. The window is turned into RESIZABLE after login. If user logs out the window is changed back to the initial size without RESIZABLE flag. Everything is OK as long as the user logs out from normal window, but when user hits the maximize button and then logs out in some distros the window still stays maximized and the login screen is being painted in a top left corner of the window.

问题来了,有没有办法检测窗口是否已最大化,以便我可以在缩小尺寸之前将其取消最大化?

And here comes the question, is there a way of detecting whether the window has been maximized so I can de-maximize it before sizing down?

我在 pygame 文档或在线任何地方都找不到任何可以帮助我解决此问题的内容.我找到了一种使用以下方法获取窗口句柄"的方法:

I couldn't find anything that would help me with this in the pygame docs or anywhere online. I have found a way of getting a "handle" to the window by using:

pygame.display.get_wm_info()['window']

但不知道从哪里开始.

我设置尺寸的方式:

self.screen = pygame.display.set_mode((800, 480)) #login screen
self.screen = pygame.display.set_mode(user_saved_size, pygame.RESIZABLE) #game screen

推荐答案

get_wm_info()['wmwindow'] 在 Windows 管理器 (X.org) 中为您提供 windowID,但它是外部"的游戏.也许使用 python 库 Xlib 你可以做点什么.

get_wm_info()['wmwindow'] gives you windowID in Windows Manager (X.org) but it is "outside" of PyGame. Maybe with python library Xlib you could do something.

我在设置正在运行的应用程序的窗口尺寸中尝试了示例以更改终端大小,但它可以工作,但它不起作用't 更改 PyGame 窗口大小.我试过 xlib 来获取 PyGame 窗口标题,它可以工作,但我无法设置 PyGame 窗口标题.看来 PyGame 不尊重新标题.

I tried example in Setting the window dimensions of a running application to change terminal size and it works but it don't change PyGame window size. I tried xlib to get PyGame window caption and it works but I could not set PyGame window caption.It seems PyGame doesn't respect new caption.

我使用这段代码来测试 PyGame 窗口标题 - 它可以获取标题但不能设置标题.

I use this code to test PyGame window caption - it can get caption but it can't set caption.

import sys

import pygame
from pygame.locals import *

import Xlib
import Xlib.display

WIDTH, HEIGHT = 1500, 300

pygame.init()

screen = pygame.display.set_mode((800,600),0,32)

print "wm_info:", pygame.display.get_wm_info()
print "  window:", pygame.display.get_wm_info()['window']
print "fswindow:", pygame.display.get_wm_info()['fswindow']
print "wmwindow:", pygame.display.get_wm_info()['fswindow']

display = Xlib.display.Display()
root = display.screen().root

#windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
#print "Xlib windowID:", windowID
#window = display.create_resource_object('window', windowID)

window = display.create_resource_object('window', pygame.display.get_wm_info()['window'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib   window get_wm_name():", window.get_wm_name()

window = display.create_resource_object('window', pygame.display.get_wm_info()['fswindow'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib fswindow get_wm_name():", window.get_wm_name()

window = display.create_resource_object('window', pygame.display.get_wm_info()['wmwindow'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib wmwindow get_wm_name():", window.get_wm_name()

print

print "Xlib wmwindow set_wm_name(hello world of xlib)"
window.set_wm_name("hello world of xlib")
display.sync()

print "Xlib wmwindow get_wm_name():", window.get_wm_name()


# --------------

fpsClock = pygame.time.Clock()
RUNNING = True
while RUNNING:

    for event in pygame.event.get():
        if event.type==QUIT:
            RUNNING = False

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                RUNNING = False

    fpsClock.tick(25)

# --------------

pygame.quit()
sys.exit()

我使用此代码更改窗口大小 - 它适用于终端和 DreamPie (python shell):

I use this code to change window size - it works in terminal and DreamPie (python shell):

# https://unix.stackexchange.com/questions/5999/setting-the-window-dimensions-of-a-running-application

WIDTH, HEIGHT = 1500, 300
import Xlib
import Xlib.display

display = Xlib.display.Display()
root = display.screen().root
windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
window = display.create_resource_object('window', windowID)
window.configure(width = WIDTH, height = HEIGHT)
display.sync()

#windowIDs = root.get_full_property(display.intern_atom('_NET_CLIENT_LIST'), Xlib.X.AnyPropertyType).value
#for windowID in windowIDs:
#   window = display.create_resource_object('window', windowID)
#   name = window.get_wm_name() # Title
#   pid = window.get_full_property(display.intern_atom('_NET_WM_PID'), Xlib.X.AnyPropertyType) # PID

这篇关于如何在 pygame 中检测可调整大小的窗口状态并在 Linux 中将其取消最大化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆