How do i program a button in python (without Tkinter)?

Question:

I’ve recently started learning Python3 and I was trying to make a game with Python3 by importing pygame. I tried to make a menu and I am having some struggles with it. I just tried to make it seem like its a button by letting the rectangle change color when you hover over it but its not working. I already tried some things but its not working. Anyhow here is the full code: hastebin link.

This is the part where I tried to make a button:

def game_intro():
    intro = True

    gameDisplay.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf', 90)
    TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    mouse = pygame.mouse.get_pos()

    if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
        pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
    else:
        pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))

    smallText = pygame.font.Font('freesansbold.ttf' ,20)
    textSurf, textRect = text_objects("START!", smallText)
    textRect.center = ( (150+(100/2)), (450+(430/2)) )
    gameDisplay.blit(textSurf, textRect)

    pygame.draw.rect(gameDisplay, red, (550, 430, 100, 50))

    pygame.display.update()
    clock.tick(15)

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
Asked By: CheekyEntity

||

Answers:

The problem here is that you’re only doing your mouseover test once, at the start of the function. If the mouse later moves into your rectangle, it won’t matter, because you never do the test again.

What you want to do is move it into the event loop. One tricky bit in PyGame event loops is which code you want to run once per event (the inner for event in… loop), and which you only want to run once per batch (the outer while intro loop). Here, I’m going to assume you want to do this once per event. So:

def game_intro():
    intro = True

    # ... other setup stuff 

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

            mouse = pygame.mouse.get_pos()

            if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
                pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
            else:
                pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))

It looks like some of the other stuff you do only once also belongs inside the loop, so your game may still have some problems. But this should get you past the hurdle you’re stuck on, and show you how to get started on those other problems.

Answered By: abarnert

Here is a button that should suit your needs:

class Button(object):
    global screen_width,screen_height,screen
    def __init__(self,x,y,width,height,text_color,background_color,text):
        self.rect=pygame.Rect(x,y,width,height)
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.text=text
        self.text_color=text_color
        self.background_color=background_color

    def check(self):
        return self.rect.collidepoint(pygame.mouse.get_pos())

    def draw(self):
        pygame.draw.rect(screen, self.background_color,(self.rect),0)
        drawTextcenter(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)  
        pygame.draw.rect(screen,self.text_color,self.rect,3)

Use the draw function to draw your button, and the check function to see if the button is being pressed.

Implemented into a main loop:

button=Button(x,y,width,height,text_color,background_color,text)
while not done:
    for event in pygame.event.get():
            if event.type==QUIT:
                terminate()
            elif event.type==pygame.MOUSEBUTTONDOWN:
                if button.check():
                       #what to do when button is pressed

    #fill screen with background
    screen.fill(background)

    button.draw()

    pygame.display.flip()
    clock.tick(fps)
Answered By: gnawydna

This is what i did and it works now:

def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            # print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

    gameDisplay.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf', 90)
    TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    mouse = pygame.mouse.get_pos()

    # print(mouse)

    if 150 + 100 > mouse[0] > 150 and 450 + 50 > mouse[1] > 450:
        pygame.draw.rect(gameDisplay, bright_green, (150, 450, 100, 50))
    else:
        pygame.draw.rect(gameDisplay, green, (150, 450, 100, 50))
    pygame.draw.rect(gameDisplay, red, (550, 450, 100, 50))
    pygame.display.update()
    clock.tick(15)

Thank you for the help @abarnert

Answered By: CheekyEntity

Does anyone have one i Can copy and paste or help me

Answered By: BracstonCoding
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.