Having a pygame.display window open, I call pygame.display.quit() upon it in order to destroy the window.
Because I need to open the window again, I call pygame.display.init() and pygame.display.set_mode(), but after these two functions are called, nothing happens.
Can anyone point me to the root of this problem?
Here is example code with a gui module... Whenever you call screen_off() then the display quits. Whenever you want display to come back, type everything you used before to turn it on.
If you want, use pygame.display.quit(), without it being inside the screen_off() function. I suggest taking all the code you used to get the display on, and putting it into a function so you don't have to type it again to turn it on after it's been killed.
from pygame import *
from pygame.locals import *
import pygame, pygame.locals
from easygui import *
def screen_off():
pygame.display.quit()
pygame.init()
canvas = pygame.display.set_mode((400,400),0,32)
red = (255,0,0)
canvas.fill(red)
pygame.display.update()
screen_off() #display is now OFF...
choice = ['Yes', 'No']
cc = buttonbox('Continue?', "Options", choice)
if cc == "Yes":
#if you don't want to type these arguments below again to turn on the display then
#put them into a function and call it
pygame.init()
canvas = pygame.display.set_mode((400,400),0,32)
purple = (204,0,204)
canvas.fill(purple)
pygame.display.update()
#display is now ON...
It should be:
pygame.init()
so i asssume that:
pygame.quit()
works the same
Have you tried calling just pygame.quit() or pygame.init()? I don't believe there is a pygame.display.quit().
Related
I'm currently making a graph plotter and I'm in the early stages so Im just using the shell to get inputs from the user at the moment. However, due to other parts of my program, I need the pygame window to be open before they begin their inputs (I cannot change the order of this as their inputs are gotten by a function and I don't really want to open pygame in this function). This blocks the shell so I used pygame.display.iconify() which minimized the pygame window doing what I needed.
My problem is that when you have completed the inputs the pygame window is still minimized and I want it to be back as an active window. Is there such a thing that does the opposite of iconify() or should I change my code completely?
Thanks.
There's no way to do this with pygame only, but if you're on Windows, you can use the pywin32 package.
Here's an example that will minify and restore the window every second:
import pygame
import win32gui
import win32con
def main():
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((200, 200))
hwnd = win32gui.GetForegroundWindow()
EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(EVENT, 1000)
while True:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
return
if event.type == EVENT:
if win32gui.IsIconic(hwnd):
win32gui.ShowWindow(hwnd, win32con.SW_SHOWNOACTIVATE)
win32gui.BringWindowToTop(hwnd)
else:
pygame.display.iconify()
screen.fill((30, 30, 30))
clock.tick(30)
pygame.display.flip()
main()
Don't use pygame.display.iconify() as there is no pygame.display.uniconify() counterpart.
There is this ugly hack, where you can use pygame.display.set_mode() to hide the window, and then regain focus and size, when needed:
# Minimize window
pygame.display.set_mode((1,1))
# Restore window
pygame.display.set_mode((1024, 768))
You should write your plot on a surface, independent of the display surface, and then blit and flip it, when you need to display it.
When i run this pygame program, a window is supposed to pop up, but nothing happens. Am i doing something wrong?
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
The problem here is that the program ends and closes the window. You should create some sort of game loop to keep the window open. An easy fix is to use an infinite while loop after the last line.
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
while True:
pass
import pygame
import sys
from pygame.locals import *
DISPLAY_SURF = pygame.display.set_mode((640,480))
#Sets the resolution to 640 pixels by 720 pixels
class Game:
def __init__(self):
pygame.init()
self.FPS = 60
self.fps_clock = pygame.time.Clock()
self.surface = pygame.display.set_mode((640, 480))
pygame.display.set_caption("The Hunt")
img = pygame.image.load("Graphics/background.png")
self.surface.blit(img)
#This class sets the basic attributes for the window.
#The clock is set to 60 and the name of the window
#is set to The Hunt which is a working title for my project
def run(self):
while True:
pygame.display.update()
self.fps_clock.tick(self.FPS)
self.process_game()
#This updates the window display to refresh every clock tick
def process_game(self):
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
game = Game()
#This creates an instance of the class Game which gives all the attributes
# and behaviours to this instance
game.run()
#Calling this function generates a window with the attributes defined.
I need some help. I have already checked if it is in the same folder, the file is definitely a png and I spelt all the folder names and the destination correctly. I'm open to any suggestions
I'm going to answer this question despite that it is not really a good one for Stack Overflow. On this site, you'll have to be more specific and detailed, because no one intends to read through a huge lot of code for you. I did however pickup some things that I think can be fixed ( some of these are opinion based, something that your question should never force an answer to have), but... here it is anyway:
For starters, when you construct a class, you use parenthesis after the class name, even if it's not going to inherit anything form another class. So change the line where you construct the Game class to this:
class Game():
Second thing about this code is that if your going to create the pygame window surface inside the Game() class, I don't understand why you're creating another window at the beginning of your code. If there is a reason for this please explain it in a comment in your code.
The last thing is more opinion-based . I don't know how many people create Pygame GUI applications like this, but it would be simpler to not use classes so you could understand the code better. When I create a Pygame GUI, I define the window, then the sprites, then I run the main game loop in a While Loop. Here is how I would normally structure your program:
#import pygame
import pygame, sys
from pygame.locals import *
#Initialize pygame and define colours
pygame.init()
white = 255,255,255
#Sets the resolution to 640 pixels by 720 pixels and caption for pygame window
DISPLAY_SURF = pygame.display.set_mode((640,480))
pygame.display.set_caption("The Hunt!")
#Create a clock object
clock = pygame.time.Clock()
FPS = 60
#Define a variable to refer to image
image = pygame.image.load("download.jpg")
#Start main loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAY_SURF.fill(white)
DISPLAY_SURF.blit(image,(0,0))
pygame.display.update()
I do use classes when creating a sprite, so I can create several instances of it, as well as keep functions I want to perform on the sprite all in one place. Doing this for the WHOLE program does work, and I suppose it's more "pythonic" (since python is object-oriented) but is still unnecessary for something like this. Here is a reference that teaches pygame in a similar way to how I code it, which I personally find to be an excellent tutorial.
Many people also put this code in a main() function and then run it, which is also a wildly used and accepted practice.
I´m a newbie on pygame, and i have this code for trying to do kind of a startscreen for a football game, this start screen has a static background image that is"img_start_screen" and the logo of the game that I want it to have a little animation that stops when it reaches the certain point on the screen, that logo is the variable "logo"
import pygame,sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((1280,720))
pygame.display.set_caption("CEFoot 3.0")
FPS=60
fpsClock=pygame.time.Clock()
img_start_screen=pygame.image.load("start_screen.gif")
logo=pygame.image.load("logo8bit.png")
logox=390
logoy=0
direction="down"
while True:
window.blit(img_start_screen,(0,0))
if direction == "down":
logoy+=5
if logoy==300:
direction="center"
if direction == "center":
pass
screen.blit(logo,(logox,logoy))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update
fpsClock.tick(FPS)
The problem is that when I run the code it starts as a black screen and not until I move the window "out" of the Windows screen it refreshes, the images aren´t the problem as I ran the code without them and it showed the same black screen. I´m beginning to think that it isn´t code problem but compability as I´m running Python 3.2 and Pygame 1.9(I think) the last version that is really old , all this on Windows 10, so this might be the problem.Thanks in advance.
As you know pygame.display.update is a function
as seen if you print it:
>>> print pygame.display.update
<built-in function update>
so when you call pygame.display.update without parenthesis () you are merely stating a reference to a function. Whereas if you use parenthesis you call the function:
>>> print pygame.display.update()
None
this calls the function and returns None
I am trying to retrieve the color I have drawn onto my display using pygame, but I can't seem to get it to work. I took out some irrelevant code for easier reading, but here is what I have.
import pygame
import sys
from pygame.locals import *
pygame.init()
blue = (0,0,255)
#sets up screen
setDisplay = pygame.display.set_mode((400,400))
pygame.display.set_caption('Connections')
pygame.draw.circle(setDisplay, blue, (20,20), 10, 10)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
print pygame.setDisplay.get_at((20,20))
Whenever I run this code, I get the following error:
TypeError: descriptor 'get_at' requires a 'pygame.Surface' object but received a 'tuple'
setDisplayis a variable you created, not an attribute on pygame. Try just setDisplay.get_at((20,20)). I'm not sure why you are even getting at the descriptor, it seems like you should be getting an AttributeError on pygame.setDisplay before even calling the get_at descriptor, but in any case, you should be calling it against your setDisplay, not off an attribute of pygame.