Python Forum
[PyGame] Can't get button class to work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Can't get button class to work
#1
Hi - me again, haha!

I found a button class I understand - this one - but now I've encountered a problem. When a button is clicked, I want it to run a function that opens a new screen. However, the new function only runs whilst I'm keeping hold of the mouse. I could fix it by making the button set something to true which would then run the function, however now that I am using a function, it won't work and none of us here can figure out why. Any help?

This is where I've first tried to use the classes. I put the 'if clicked == False' to get the original screen to stop showing when a function is run - in each function, it sets clicked to True at the beginning - but now it still doesn't work. Any help?
# MAIN GAME LOOP

while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    if clicked == False:

        print("opened")
        print(clicked)

        screen.fill(WHITE)

        # initialising all buttons
        teacherLogin = Button((100, 250, 400, 100), teacherChoice, text = 'Teacher')
        studentLogin = Button((100, 100, 400, 100), letterButton, text = 'Student', colour = BLUE)

        teacherLogin.getEvent()
        teacherLogin.draw(screen)

        studentLogin.getEvent()
        studentLogin.draw(screen)
        print("drew buttons")

        screen.blit(loginMessage, [100, 10])
        print("showed title")

        pygame.display.update()

    elif clicked == True:
        print("clicky")
        print(teacherLogin.function)

pygame.quit()
    
It doesn't run the 'clicky' bit which should run after clicked becomes true either.

If you need more of the code to be able to help, is there any chance I could send you it via PM? I don't particularly want to post my entire project code so far online but I know this might not be enough for you to be able to help. Thanks!
Reply
#2
I'm not quite sure what you are doing with the clicked flag. Regardless, you need to move the initialization of the buttons outside of the while loop because you are creating a new button every frame. Also the get event methods have to be ran under the event loop.

I can give an example when I get to a desktop instead of this small phone. Compare the first code snippet in the tutorial of these issues with your code shown here.
Recommended Tutorials:
Reply
#3
The clicked flag was originally needed to make sure the function would continue to run, not only when the button was being held down - this was the only way I could fix it. I'm confused, as I thought it had to be in the loop to ensure it would let you close the program without crashing? I really appreciate the help, thank you.
Reply
#4
(Nov-26-2018, 04:45 PM)mzmingle Wrote: I thought it had to be in the loop to ensure it would let you close the program without crashing?
The callback function gets executed the second you press the button. Im not sure what you mean by new screen, but any result will occur after the callback is executed. You can use a flag such as your clicked value to determine if something needs to occur persistently based on the button being clicked or not, but not the button logic itself.

Here is an example using your code:
screen = pygame.display.set_mode((800,600))
done = False
def teacherChoice():
    print('executing techerChoice')
    
def letterButton():
    print('executing letterButton')

teacherLogin = Button((100, 250, 400, 100), teacherChoice, text = 'Teacher')
studentLogin = Button((100, 100, 400, 100), letterButton, text = 'Student', color = pg.Color('blue'))

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        teacherLogin.getEvent(event)
        studentLogin.getEvent(event)
 
    screen.fill(pg.Color('white'))
    teacherLogin.draw(screen)
    studentLogin.draw(screen)
    pygame.display.update()
 
pygame.quit()
The difference between ours is
  • the initialization of the buttons are before the main loop
  • the buttons' event method is ran in the event loop
  • there are callback functions for the buttons

The callback function are only ran once after being pressed. They themselves can call a flag variable to do something persistently. A "new screen" to me sounds like a state machine. If you want this to occur with a button press, then you can use this example and replace the event of mouse button down event to your button press event instead. In this example the buttons callback function would just have to set the states done value to True to get the next screen.

I made this example for this thread, but would be better suited in the tutorials section for future users..
https://python-forum.io/Thread-PyGame-Cr...4#pid64464
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Button class still not working? mzmingle 3 3,052 Jan-16-2019, 04:43 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020