【发布时间】:2014-07-21 08:59:30
【问题描述】:
基本上,我正在编写一个看起来有点像 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()
如您所见,我仍然是编程方面的业余爱好者,因此对于理解上面编写的代码可能有多么困难,我深表歉意。如果我需要澄清任何事情或回答任何问题,请告诉我,我会尽力回答!
提前致谢!
~大敲
【问题讨论】:
-
首先 butmax 永远不会计算 - 只是比较,然后您对 x 轴的计算是使用按钮总数而不是适合的实际数字。基本上你的逻辑很遥远——你需要重新考虑计算——方法是正确的。
标签: python button user-interface pygame launcher