[英]How to make a “Quit” button click event to close a program using PyGame
I'm working for the first time with the Pygame library and I'm having some trouble in making a simple button, whose click event will close my program.我第一次使用 Pygame 库,我在制作一个简单的按钮时遇到了一些麻烦,其点击事件将关闭我的程序。 I've segmented my code in the follow way:
我按以下方式分割了我的代码:
App.py, which contains my mainloop: App.py,其中包含我的主循环:
import pygame
from pygame.locals import *
from widgets import *
class App():
# creates object
def __init__(self):
self._running = True
self._display_surf = None
self.size = self.weight, self.height = 1000, 500
self.button_1 = rect_Button(((255,255,255)),(500,250),"Quit")
# initializes all PyGame modules
# create main display & uses hardware acceleration
def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._running = True
def on_event(self, event):
if event.type == pygame.QUIT:
self._running = False
#if event.type == MOUSEBUTTONUP:
#self._running = False
if self.button_1.click_State:
# testing button
print("closing...")
def on_loop(self):
pass
def on_render(self):
rect_button_1 = pygame.draw.rect(self._display_surf,self.button_1.color,self.button_1.container())
self._display_surf.blit(self.button_1.display_Text,rect_button_1)
pygame.display.update()
def on_cleanup(self):
pygame.quit()
def on_click(self):
pass
def on_execute(self):
if self.on_init() == False:
self._running = False
while( self._running ):
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
self.on_render()
self.on_cleanup()
widgets.py, where i'll create classes for widgets which aren't built-in on Pygame: widgets.py,我将为 Pygame 上未内置的小部件创建类:
import pygame
from pygame.locals import *
pygame.init()
class rect_Button():
def __init__(self, color, pos, text):
self.font = pygame.font.Font('freesansbold.ttf',32)
self.display_Text = self.font.render(text,True,((200,0,0)),None)
self.mouse = pygame.mouse.get_pos()
self.click = pygame.mouse.get_pressed()
self.width = self.font.size(text)[0]
self.height = self.font.size(text)[1]
self.color = color
self.pos_X = pos[0]
self.pos_Y = pos[1]
def container(self):
return pygame.Rect(self.pos_X,self.pos_Y,self.width,self.height)
def click_State(self):
if self.pos_X+self.width > mouse[0] > self.pos_X and self.pos_Y+self.height > mouse[1] > self.pos_Y:
if self.click[0]==True:
return True
else:
return False
And finally game.py, where I create the App object:最后是 game.py,我在其中创建了应用程序 object:
import pygame
from pygame.locals import *
from app import *
if __name__ == "__main__" :
theApp = App()
theApp.on_execute()
My problem is that I want to click on the Quit button I've created and close my program.我的问题是我想点击我创建的退出按钮并关闭我的程序。 To test it, I made it print "closing..." whenever I clicked within the button.
为了测试它,每当我在按钮内单击时,我都会让它打印“关闭...”。 However, it continuously prints whenever I move my mouse inside the program window.
但是,每当我在程序 window 中移动鼠标时,它就会连续打印。 How should I tell my code to get from the button object the information that the button was clicked, run the event and close the window?
我应该如何告诉我的代码从按钮 object 获取单击按钮的信息,运行事件并关闭 window?
Thank you谢谢
Its really hard to follow your code but I would advise creating a separate function which deals with checking for events.很难遵循您的代码,但我建议创建一个单独的 function 来处理事件检查。 When you want to check for a press of a button its easier to use the following under your check
pygame.event.get()
loop:当您想检查是否按下按钮时,在检查
pygame.event.get()
循环下更容易使用以下命令:
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
button_clicked = easy_button.rect.collidepoint(mouse_x, mouse_y)
if button_hard_clicked and self._running: # checks of active program/click
# Whatever you want to do if the button is clicked
self._running = False
Check out my Star Wars pygame shooting program, which also implements a button.查看我的星球大战 pygame 拍摄程序,它也实现了一个按钮。 Hope this will sort of help.
希望这会有所帮助。 The code for the button will be found in
game_functions.py
and main.py
files.按钮的代码可以在
game_functions.py
和main.py
文件中找到。 Here is the link链接在这里
As of now,截至目前,
if self.button_1.click_State:
will not call click_State() To fix this, add () to the end of click_State to call the function.不会调用 click_State() 要解决此问题,请在 click_State 末尾添加 () 以调用 function。 Additionally, when the line is written as above, the rest of the statement will automatically carry on.
另外,如上写行时,语句的rest会自动进行。 (hence all of the "closing...")
(因此所有的“关闭......”)
if self.button_1.click_State():
The above line is closer, but the program is not checking to see if it matches anything.上面的行更接近,但程序没有检查它是否匹配任何内容。 To fix this, change to:
要解决此问题,请更改为:
if self.button_1.click_State() == True:
From there you can insert the rest of the code to close the program.从那里您可以插入代码的 rest 以关闭程序。 Hope I could be of help!
希望我能有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.