简体   繁体   中英

Pygame choose which display to use in fullscreen

This is the code:

#!/usr/bin/python

import pygame, sys
from pygame.locals import *
import Tkinter as tk

root = tk.Tk()

pygame.init()

w = 640
h = 400

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
PINK = (255, 0, 255)
CYAN = (0, 255, 255)
WHITE = (255, 255, 255)

screen = pygame.display.set_mode((w,h), RESIZABLE)
clock = pygame.time.Clock()
x = y = 100

tbar = False

Lbutton = Mbutton = Rbutton = MouseX = MouseY = None

logo = pygame.image.load("C:\\Users\\chef\\Desktop\\slogo.png").convert()

while 1:
    for event in pygame.event.get():
        screen.fill(WHITE)
        MouseX, MouseY = pygame.mouse.get_pos()
        Lbutton, Mbutton, Rbutton = pygame.mouse.get_pressed()
        pygame.draw.rect(screen, GREEN, ((0, h-40),(w,h)))
        if event.type == pygame.QUIT:
            sys.exit()  
        elif event.type==VIDEORESIZE:
            w = event.dict['size'][0]
            h = event.dict['size'][1]
            screen=pygame.display.set_mode(event.dict['size'],RESIZABLE)
        elif h - 50 < MouseY < h and MouseX <= 40 and Lbutton or screen.get_at(pygame.mouse.get_pos()) == (255, 0, 0, 255):
            tbar = True
        elif MouseY < h-50 or MouseX > 40 or Lbutton == False:
            tbar = False
        if tbar == True:
            pygame.draw.rect(screen, RED, ((0, h/2), (w/5, h/2-40)))
        if event.type is KEYDOWN and event.key == K_f:
            if screen.get_flags() & FULLSCREEN:
                w = 640
                h = 400
                pygame.display.set_mode((w,h))
            else:
                w = root.winfo_screenwidth()
                h = root.winfo_screenheight()
                pygame.display.set_mode((w,h), FULLSCREEN)
    screen.blit(logo, ((0, h-40),(40, h)))
    pygame.display.flip()
    clock.tick(60)

When I press the key F, it toggles fullscreen. However, if I run the program, move the window to the 2nd monitor, then press F, it shows fullscreen on the 1st monitor. How can I select which monitor to show fullscreen?

What I know about pygame (1.9.x) / SDL < 2 is it's don't support dual screen configuration, so, you can't choose the screen.

It will change with pygame_sdl2, but it's not ready yet.

Indeed, your code should reaffect screen :

        if screen.get_flags() & FULLSCREEN:
            w = 640
            h = 400
            screen = pygame.display.set_mode((w,h), RESIZABLE)
        else:
            w = root.winfo_screenwidth()
            h = root.winfo_screenheight()
            screen = pygame.display.set_mode((w,h), FULLSCREEN)

For my dual screen configuration, pygame do some strange things, toggle to fullscreen do fullscreen on screen 1 (it don't detect fullscreen usage in this case), and next on two screen, (this time, fullscreen mode is detected, and so) and next return to windowed mode...

Maybe, try pyglet to do what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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