preface
Now more and more people use pygame to make small games, but pygame has no pop-up mechanism
Generally, we will use tkinter library or pgu library to solve this problem
The pgu library has not been introduced in a manual suitable for novices, but only the examples and descriptions of some functions in the download file. Therefore, this paper mainly introduces the two ways that pgu is triggered by buttons and setting events
In addition, it also solves the problem of pop-up window under the pygame window
1, What is pgu?
The full name of pgu is Phil's pyGame Utilities. It is a set of modules and scripts of pygame, including some small modules integrated with gui
Download address
Download address
pgu home page
2, Use steps
1. Installation Library
Open cmd under windows10 and enter PIP install pyGame PGU
You can also download the package directly from the above address
2. Make button pop-up window
The code is as follows (example):
Import and warehousing
import pygame import random import pgu from pgu import gui,timer
First use pygame to generate a 500 * 500 window. There are many pop-up windows in the pgu example, but there are few pop-up windows in the windows generated by pygame
pygame.init() screencaption = pygame.display.set_caption('chess') screen = pygame.display.set_mode((500, 500))
Construct a custom dialog class
class TestDialog(gui.Dialog): def __init__(this): title = gui.Label("Some Dialog Box") #Pop up title label = gui.Label("Close this window to resume.") #Pop up content gui.Dialog.__init__(this, title, label)
For initialization, pgu had better generate a container to store the buttons that need to be placed. Finally, remember init initialization
app = gui.App() #Initialize gui c = gui.Container(align=-1,valign=-1) #Container for generating gui abc = TestDialog() #Generate pop-up abc btn = gui.Button("a") #Generate button with text a btn.connect(gui.CLICK, abc.open, None)#Bind the button to the pop-up of the pop-up window c.add(btn,0,0) #Place the button in the container (0,0) app.init(c)
First generate a 500 * 500 window with pygame, and then import the pop-up elements of pgu
pygame.init() screencaption = pygame.display.set_caption('chess') screen = pygame.display.set_mode((500, 500))
The main function is the event loop acquisition mechanism of pygame, but it should be noted that app The event function conveys the events of pygame to pgu, otherwise pgu commands cannot be executed
while True: for e in pygame.event.get(): if e.type is pygame.QUIT: pygame.quit() else: app.event(e) #It is important to pass pygame events to pgu screen.fill((0,0,0)) #Generate a screen app.paint() #Draw the contents of the pgu container pygame.display.update()
3. Create event trigger pop-up window
In addition to triggering events through the buttons on the interface, we often need to trigger the pop-up window by an event. Here, the variable a is set. Press the button a every time to increase 1 until a is greater than 5 to trigger the pop-up event
Import and warehousing
a = 0
Bind the auto increment function of a to the button
def add(self): global a a = a + 1 btn.connect(gui.CLICK, add, None)#Bind the button to the pop-up of the pop-up window
Add the judgment status function, and a pop-up window will pop up when it is judged to be True
if a > 5: abc.open() a = 0
4. Complete code of two modes
Button trigger pop-up
import pygame import random import pgu from pgu import gui,timer class TestDialog(gui.Dialog): def __init__(this): title = gui.Label("Some Dialog Box") label = gui.Label("Close this window to resume.") gui.Dialog.__init__(this, title, label) pygame.init() screencaption = pygame.display.set_caption('chess') screen = pygame.display.set_mode((500, 500)) app = gui.App() #Initialize gui c = gui.Container(align=-1,valign=-1) #Container for generating gui abc = TestDialog() #Generate pop-up abc btn = gui.Button("a") #Generate button with text a btn.connect(gui.CLICK, abc.open, None)#Bind the button to the pop-up of the pop-up window c.add(btn,0,0) #Place the button in the container (0,0) app.init(c) while True: for e in pygame.event.get(): if e.type is pygame.QUIT: pygame.quit() else: app.event(e) #It is important to pass pygame events to pgu screen.fill((0,0,0)) #Generate a screen app.paint() #Draw the contents of the pgu container pygame.display.update()
Event triggered pop-up code
import pygame import random import pgu from pgu import gui,timer class TestDialog(gui.Dialog): def __init__(this): title = gui.Label("Some Dialog Box") label = gui.Label("Close this window to resume.") gui.Dialog.__init__(this, title, label) pygame.init() screencaption = pygame.display.set_caption('chess') screen = pygame.display.set_mode((500, 500)) global a a = 0 app = gui.App() #Initialize gui c = gui.Container(align=-1,valign=-1) #Container for generating gui abc = TestDialog() #Generate pop-up abc btn = gui.Button("a") #Generate button with text a def add(self): global a a = a + 1 btn.connect(gui.CLICK, add, None)#Bind the button to the pop-up of the pop-up window c.add(btn,0,0) #Place the button in the container (0,0) app.init(c) while True: for e in pygame.event.get(): if e.type is pygame.QUIT: pygame.quit() else: app.event(e) #It is important to pass pygame events to pgu screen.fill((0,0,0)) #Generate a screen app.paint() #Draw the contents of the pgu container if a > 5: abc.open() a = 0 pygame.display.update()
When I tried, I found that:
1. If there is no screen The pop-up window of the fill function cannot be closed
2. If there is no app Event, the pgu event cannot be executed
summary
pgu library has less online content, and it will be more stable than tkinter when used in pygame. However, the hard injury of pgu can not display the Chinese interface, so it is better to use tkinter in this regard