Basically i am writing a program that looks sort of like a control panel in python/pygame. Its purpose is to read a number of files with a certain file extension, count them, and then draw a number of buttons on the control panel according to the corresponding amount of files counted.
I have achieved this, but my one problem is that i need to be able to make the buttons form in organized rows within the restrictions of the given screen. However, I am not quite sure how to approach this so i decided to try stackoverflow to see if anybody has been able to overcome this endeavor before.
The code is as follows:
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
x = 9 #button's original x coordinate
y = 20 #button's original y coordinate
butrow = 0 #Counter for how many buttons that can fit inside of the given screen space
butmax = 0 #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 x < Screensize - (Screensize/int(len(gamesnum))):
lebutton=GUI_Helper(white, red9, x, y)
lebutton.Standard_button_make(button_size, button_size-2)
x += (button_size + 2)
butrow += 1
#When maximum amount of buttons that can fit across the screen is reached
if butrow == butmax:
butrow = 0 #Reset the counter
x = 9 #Reset button's x coordinate
y += (button_size + 2) #Move next buttons down a row
pygame.display.update()
So as you can see, i am still quite the amateur at programming and as such i truly apologize for how hard it may be to understand my code written above. If I need to clarify anything or answer any questions, let me know and i will do my best to answer them!
Thanks in advance!
~Dachua
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()