Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

python - PyGame os.environ SDL_VIDEO_WINDOW_POS does not work

I'm trying to set the position of my window in PyGame, but this method doesn't work for me. It says that it's updated when I print it out, but nothing changes visually.

My code:

    import pygame as pg
from tkinter import messagebox as mb
import time
import sys
import requests
import os

pg.init()

sc = pg.display.set_mode((1000,750))
try:
    testimg143 = open("Data\test.txt", "r")
except:
    ud = "ginfo\"
else:
    ud = ""
    testimg143.close()

#main game loop

gamewindowfullscreen = False

def setgamewindowcenter():
    x = 500
    y = 100
    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
    print(os.environ['SDL_VIDEO_WINDOW_POS'])

while True:
    for event in pg.event.get():
        if (event.type == pg.KEYUP):
            if (event.key == pg.K_F11):
                if (gamewindowfullscreen == False):
                    sc = pg.display.set_mode((0,0), pg.FULLSCREEN)
                    setgamewindowcenter()
                    gamewindowfullscreen = True
                else:
                    sc = pg.display.set_mode((1000,750))
                    setgamewindowcenter()
                    gamewindowfullscreen = False
                    
        if event.type == pg.QUIT:
                pg.quit()
                exit()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With earlier version of Pygame (before 2.0.0), the window position could be change by setting SDL_VIDEO_WINDOW_POS and calling pygame.display.set_mode() (at least at some systems.
This is no longer possible. If you want to change the position of the window you have to reinitialize the display module pygame.display.quit() and pygame.display.init().
In any case SDL_VIDEO_WINDOW_POS needs to be set before calling pygame.display.set_mode().

Minimal example:

import pygame as pg
import os

def setgamewindowcenter(x = 500, y = 100):
    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
    print(os.environ['SDL_VIDEO_WINDOW_POS'])

pg.init()
setgamewindowcenter()
sc = pg.display.set_mode((1000, 750))

gamewindowfullscreen = False
run = True
while run:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False

        if event.type == pg.KEYUP:
            if event.key == pg.K_F11:
                if gamewindowfullscreen == False:
                    sc = pg.display.set_mode((0,0), pg.FULLSCREEN)
                    gamewindowfullscreen = True
                else:
                    pg.display.quit()
                    setgamewindowcenter()
                    pg.display.init()
                    sc = pg.display.set_mode((1000, 750))
                    gamewindowfullscreen = False
                    
pg.quit()
exit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...