【问题标题】:Pygame_widgets button does not execute functionsPygame_widgets 按钮不执行功能
【发布时间】:2021-10-27 00:44:48
【问题描述】:

我正在尝试在 python 中创建一个按钮,我尝试使用 Pygame_widgets 库,但我的函数不会执行

import basic
import pygame as pg
import pygame_widgets as pw
from pygame_widgets.button import Button

def startWindow():
 pg.init()
 win = pg.display.set_mode((600, 600))

 button = Button(
            win, 100, 100, 300, 150, text='Hello',
            fontSize=50, margin=20,
            inactiveColour=(255, 0, 0),
            pressedColour=(0, 255, 0), radius=20,
            onClick=lambda: basicKit = True 
            
         )
 run = True
 while run:
    events = pg.event.get()
    for event in events:
        if event.type == pg.QUIT:
            pg.quit()
            run = False
            quit()
    win.fill((255, 255, 255))
    button.listen(events)
    button.draw()
    pg.display.update()


if __name__ == '__main__':
 stop = False
 print("* Welcome To \"Drum Easy\" V 1.0 *")
 startWindow()
 while stop == False:
  kit = input("\nWhat Kit? \n\nBasic\n  ^\n  1\n\n>")
  if kit == "1":
   basic.Kit()
   stop = True

  else: print('Invalid Kit')

现在参见button = Button 部分代码有多个选项可以运行函数,但它们似乎都不起作用,它们似乎都有无效的语法

【问题讨论】:

    标签: python button pygame


    【解决方案1】:

    lambda 必须有一个表达式,而不是一个语句(赋值、if、 尽管, ...)。所以add = lambda x: x + 1 是一个有效的 lambda。但 lambda: basicKit = True 在语法上不正确。你可以改为 定义一个函数来更新你的变量并给出这个 Button 构造函数的函数。

    类似的东西:

    def update_basic_kit():
       global basicKit
       basicKit = True
    button = Button(
       win, 100, 100, 300, 150, text='Hello',
       fontSize=50, margin=20,
       inactiveColour=(255, 0, 0),
       pressedColour=(0, 255, 0), radius=20,
       onClick=update_basic_kit
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 2018-10-04
      相关资源
      最近更新 更多