2014-07-21 216 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