基本上我正在編寫一個程序,看起來有點像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