Pygame:每当我退出全屏窗口时,我的Pygame窗口总是转到右上角



每当我从全屏pygame显示器退出并返回到正常的pygame窗口尺寸时,窗口总是转到监视器屏幕的右上角。有没有方法可以指定窗口的位置

这是我的源代码:

#Setup Python---------------------------------------------------#
import pygame, sys, random, os
#Constants
WIDTH, HEIGHT = (600,400)
BLACK = (0,0,0)
#Setup pygame/window--------------------------------------------#
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('screen resize')
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.RESIZABLE)
#fullscreen
fullscreen = False
#Loop-----------------------------------------------------------#
while True:
#Background-------------------------------------------------#
screen.fill(BLACK)
#Buttons----------------------------------------------------#
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_f:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((1920, 1080),pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.RESIZABLE)


#Update-----------------------------------------------------#
pygame.display.update()
mainClock.tick(60)

我使用以下代码找到了解决方案:

from pygame._sdl2.video import Window
window = Window.from_display_module()
window.position = (100,100)

我在我的源代码中实现了如下(用<----表示(:

#Setup Python---------------------------------------------------#
import pygame, sys, random, os
from pygame._sdl2.video import Window    <-----
#Constants
WIDTH, HEIGHT = (600,400)
BLACK = (0,0,0)
#Setup pygame/window--------------------------------------------#
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('screen resize')
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.RESIZABLE)
#fullscreen
fullscreen = False
#Loop-----------------------------------------------------------#
while True:
#Background-------------------------------------------------#
screen.fill(BLACK)
#Buttons----------------------------------------------------#
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_f:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((1920, 1080),pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((WIDTH, HEIGHT),pygame.RESIZABLE)
window = Window.from_display_module() <----
window.position = (100,100)           <----
#Update-----------------------------------------------------#
pygame.display.update()
mainClock.tick(60)

最新更新