Pygame Window Closing Automatically
Closing the window We want our window to persist until the user chooses to closes it. To achieve this, we monitor user inputs (known as 'events') using pygame.event.get. This function returns a list of events which we can loop through and check to see whether any have the type QUIT. Get code examples like 'how to make a ping pong game in python' instantly right from your google search results with the Grepper Chrome Extension. PyGame, window is not closing, tut not helping. Python Forums on Bytes. Pygame.display.quit Uninitialize the display module quit - None This will shut down the entire display module. Super bounce out free. This means any active displays will be closed. This will also be handled automatically when the program exits. In Pygame 2¶ In pygame 2 the display surface is automatically resized when the window is - setmode should not be called. The VIDEORESIZE event instead lets us handle any other surfaces, or anything else, we might want to resize.
Python Opens And Closes
In this first tutorial, we'll cover the basics and write some boilerplate code for Pygame - code you will almost always write whenever you use Pygame. We will:
- Create a Pygame window
- Stop the window from immediately disappearing
- Close the window in response to a quit event
- Change the title and background colour of the window
Note for Mac users: If you get a blank screen when using Pygame, take a look here to find a version of pygame that works.
The Pygame window
Since we are going to use the Pygame module, the first thing we need to do is import it.
We can now create a Pygame window object (which I've called 'screen') using pygame.display.set_mode(). This object requires two values that define the width and height of the window. Rather than use constants, we'll define two variables, width and height, which will make the program easier to change later on. Feel free to use any integers that suit you. In order to display the window, we use the flip() function:
If you now run this program, you’ll see a 300 x 200 pixel window appear and then promptly disappear. The problem is that once as the flip() function has been called, the end of the code is reached, so the program ends.
To keep the screen visible for as long as we want, we need to make sure the program doesn't end. We could do this by adding an infinite loop.
The problem with an infinite loop is that, by definition, it never ends. The program won't quit even if we want it to. If we try to close the window by clicking on the X, nothing happens. You have to use Ctrl + C in the command line to force the program to quit.
Closing the window
We want our window to persist until the user chooses to closes it. To achieve this, we monitor user inputs (known as 'events') using pygame.event.get(). This function returns a list of events which we can loop through and check to see whether any have the type QUIT. If we find such an event, we exit our loop, which is best done by changing a boolean variable (which I've called 'running').
The window now persists whilst 'running' is equal to True, which it will be until you close the window (by clicking the X). Note that if you use an IDE for Python programming, then it may interfere with Pygame. This isn’t normally a major problem but it can stop the Pygame window from closing properly. If so, adding pygame.quit() should solve the problem (Thanks to nf3 in the comments for mentioning this).
Changing the window's properties
Now we have a usable window, we can change its properties. For example, we can change its title using the set_caption() function.
We can change the background colour by filling the screen object. Colours are defined using a 3-tuple of integers from 0 to 255, for the red, green and blue values respectively. For example, white is (255,255,255). Changes need to be made before the flip() function is called.
The final program
Pygame Window Closing Automatically Lock
The complete program, after a bit of rearrangement, should now look like this:
You can also find the complete code for this tutorial by clicking the Code on Github link at the top of the article.
Running the program should create a window that looks like this (on Windows XP).
It's not very exciting at the moment, but now we have a window that persists until we close it. In the next tutorial we'll draw some shapes in our window and start our simulation by creating a Particle object.
Close Windows automatically as they open with Window Closer. Use the software to close any popup windows or even to enable or disable buttons on application. The Window Close can be used on Windows Vista, Windows 7, Windows 8, Windows 8.1 or even on Windows 10 and the trial version can be downloaded free now.
Have a close look at the screenshot of the Window Closer as displayed on the right hand side. The Window Closer lists application windows which are open at the time the screenshot was taken. Window Closer displays non minimised windows and the Display All checkbox can be used to display all the windows open on your computer. Button, Label, Checkbox and other controls are referred to as in the document as Windows.
Download Window Closer now and give it a try. With Window Closer you can view hidden windows , enable the disabled windows such as Buttons, Checkboxes, etc. Know what all windows are running on your Windows computer right now.
Pygame Quit Event
How to Automatically Close a Window
The Software can be used to close any Window (e.g. a annoying popup message box) or any other window. In order to automatically close a window, you need to introduce that window to Window Closer Software and ask the software to automatically close it. Given below steps outline how to configure a window to be closed automatically.
- Display the Popup Window on your computer screen.
- Launch Window Closer or if the software is already running click on the Refresh button.
- Now locate the desired popup window in the top list and click on it.
- Either double click that Window record from the list or click on the Toggle Auto Close For Selected Window Above . This Action will mark that this Window must be closed automatically whenever activated. You can remove the auto closing of the Window by clicking the toggle button again. Note the last column of the Window Records button which will either say Yes for automatically closing or will be blank for not doing anything for that window.
- Now Minimize the Window Closer or Hide it to System tray using the button provided.
- Now onwards the Windows(s) marked for Auto Close will be closed automatically whenever they are activated.
Pygame Window Closes Immediately
Automation Software Downloads Home