基本上我正在编写一个程序,看起来有点像python/pygame中的控制面板。其目的是读取具有特定文件扩展名的多个文件,对它们进行计数,然后根据相应的文件数量在控制面板上绘制多个按钮。Python/Pygame:如何使按钮在屏幕上缩放按钮和屏幕大小?
我已经实现了这一点,但我的一个问题是,我需要能够在给定屏幕的限制范围内组织行中的按钮窗体。但是,我不太清楚如何解决这个问题,所以我决定尝试使用stackoverflow来查看是否有人能够克服这个问题。
的代码如下:
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()
因此,大家可以看到,我仍然在编程很业余,因此我真的为它怎么可能很难理解上面写的我的代码道歉。如果我需要澄清任何问题或回答任何问题,请告诉我,我会尽我所能来回答他们!
提前致谢!
〜Dachua
首先butmax - 只比起来,那么你的计算x轴使用按钮而不是实际的数量,将适合的总数。基本上你的逻辑很遥远 - 你需要重新思考计算 - 方法是正确的。 – gkusner