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


Messages In This Thread
Can't get button class to work - by mzmingle - Nov-21-2018, 10:39 AM
RE: Can't get button class to work - by metulburr - Nov-21-2018, 02:30 PM
RE: Can't get button class to work - by mzmingle - Nov-26-2018, 04:45 PM
RE: Can't get button class to work - by metulburr - Nov-26-2018, 10:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Button class still not working? mzmingle 3 3,153 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