gpt4 book ai didi

python - 为什么我的 print() 命令在控制台中显示 double ?

转载 作者:行者123 更新时间:2023-11-28 18:08:36 26 4
gpt4 key购买 nike

下面是我正在处理的一些代码,它们将 double 打印到 CMD 窗口。我看到 double 并不是什么大问题,我只是担心随着这个程序的发展,它迟早会变成一个更大的问题。

这是当我左键单击时 CMD 向我显示的示例:

Left mouse pressed at (451, 279)
Left mouse pressed at (451, 279)
Left mouse released at (451, 279)
Left mouse released at (451, 279)

为了简洁起见,我省略了很多代码,并将其精确定位到这些类和函数。我提前道歉 - 我对这个网站和 python 很陌生。

class Button:       
def whichButton(self):
#this function takes the mousebuttondown event, and returns WHICh button is pressed
if self.button == 1:
return "Left"
if self.button == 2:
return "Middle"
if self.button == 3:
return "Right"
if self.button == 4:
return "Wheel Up"
if self.button == 5:
return "Wheel Down"

def handleEvent(self, event):

if event.type == pygame.MOUSEBUTTONDOWN:
print( Button.whichButton(event)+' mouse pressed at ' + str(event.pos))
if self.rect.collidepoint(event.pos):
self.buttonDown = True
self.image = self.imageDown

class Game:
self.all_sprites.add(self.startButton, self.quitButton)
def run(self):
while not self.done:
self.frameRate = self.clock.tick(60) / 1000
self.handleEvent()


def handleEvent(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.done = True
for button in self.all_sprites:
button.handleEvent(event)




if __name__ == '__main__':
pygame.init()
Game(screen).run()
pygame.quit()

最佳答案

发生这种情况是因为您在 for 循环中为 self.all_sprites 组中的每个按钮调用 button.handle_event(event),所以您正在打印 print( Button.whichButton(event)+' mouse pressed at ' + str(event.pos)) 每个按钮。

def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
# Iterates over all buttons in the group and calls their `handle_event` method.
for button in self.all_sprites:
button.handle_event(event)

关于python - 为什么我的 print() 命令在控制台中显示 double ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52084840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com