w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python/Pygame: How to make buttons scale on screen with amount of buttons and screen size?

I have tried to reuse as much of your code as I could but there are a couple of big item you might want to address - you are using button_size calculated for x but not y - this will produce square buttons, and you never use any kind of text or description of the files for the buttons. That being said this code should produce the results you want:

def Button_Build(gamesnum): # 'gamesnum' is the amount of
detected files
    Screensize = 200 #size of given screen space
    button_size = Screensize/len(gamesnum) * 2   #Size of the button
according to amount of files found/size of screen

    StartX = 9
    StartY = 20
    x = StartX #button's original x coordinate
    y = StartY #button's original y coordinate        

    butrow = 0  #Counter for how many buttons that can fit inside of the
given screen space
    butmax = int(ScreenSize / (button_size + 2)) #Maximum amount of buttons
that can fit inside of given screen space

    for i in gamesnum: #Going through the number of files counted
        print(x) #Print the drawn buttons' x coordinate

        #If next button doesn't go out of given screen space
        if butrow < butmax:
            lebutton=GUI_Helper(white, red9, x, y)
            lebutton.Standard_button_make(button_size, button_size-2)
            x += (button_size + 2)
            butrow += 1
        else:
            #When maximum amount of buttons that can fit across the screen
is reached
            butrow = 0 #Reset the counter
            x = StartX #Reset button's x coordinate
            y += (button_size + 2) #Move next buttons down a row

    pygame.display.update()    




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.