2014-07-21 178 views
1

基本上我正在编写一个程序,看起来有点像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

+0

首先butmax - 只比起来,那么你的计算x轴使用按钮而不是实际的数量,将适合的总数。基本上你的逻辑很遥远 - 你需要重新思考计算 - 方法是正确的。 – gkusner

回答

0

我试图重用尽可能多的代码,因为我可以,但有几个大项目,你可能想解决 - 你正在使用button_size计算X,但不Ÿ - 这将产生方形按钮,并且您从不使用任何类型的文本或按钮文件的描述。话虽这么说,这个码应该产生的结果,你想:从不计算

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()  
+0

非常感谢帮助我发现此任务背后的逻辑 - 它几乎完美无瑕地工作,我只需调整代码的几个不同部分即可使其按原本的设计工作。不仅如此,而且你在我自己的代码中犯了错误的地方也很明显。非常感激! – Dachua