Python/Pygame:如何使按钮在屏幕上按按钮数量和屏幕大小缩放?

Python/Pygame:如何使按钮在屏幕上按按钮数量和屏幕大小缩放?,python,button,user-interface,pygame,launcher,Python,Button,User Interface,Pygame,Launcher,基本上,我正在编写一个程序,看起来有点像python/pygame中的控制面板。其目的是读取具有特定文件扩展名的多个文件,对其进行计数,然后根据计数的相应文件数量在控制面板上绘制多个按钮 我已经做到了这一点,但我的一个问题是,我需要能够使按钮在给定屏幕的限制范围内形成有组织的行。然而,我不太确定如何解决这个问题,所以我决定尝试stackoverflow,看看以前是否有人能够克服这个困难 代码如下: def Button_Build(gamesnum): # 'gamesnum' is the a

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

我已尝试尽可能多地重用您的代码,但您可能需要解决几个大问题—您使用的是为x而不是y计算的按钮大小—这将生成方形按钮,并且您永远不会为按钮使用任何文本或文件描述。也就是说,这段代码应该会产生您想要的结果:

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

首先,butmax从不计算-仅与进行比较,然后您对x轴的计算使用按钮总数,而不是适合的实际数量。基本上你的逻辑是遥远的-你需要重新思考计算-方法是正确的。非常感谢你帮助我发现这样一个任务背后的逻辑-它几乎完美无瑕地工作,我只需调整代码的几个不同部分,使其工作,因为我原本打算。不仅如此,您还使我在自己的代码中犯错误的地方变得非常明显。非常感谢!