Home > other >  Pygame_widgets button does not execute functions
Pygame_widgets button does not execute functions

Time:11-05

I am trying to make a button in python and I attempted to use the Pygame_widgets library but my functions will not execute

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')

see now in the button = Button part of the code has multiple options that can run functions but none of them seem to work, they all seem to have invalid syntax

CodePudding user response:

A lambda must have an expression, not a statement (assignment, if, while, ...). So add = lambda x: x 1 is a valid lambda. But lambda: basicKit = True is syntactically incorrect. You can instead define a function that updates your variable and gives this function to the Button constructor.

Something like that:

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
)
  • Related