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

时间:2014-07-21 08:59:31

标签: python button user-interface pygame launcher

基本上我正在编写一个类似于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

1 个答案:

答案 0 :(得分:0)

我尝试尽可能多地重用你的代码,但是你可能想要解决几个大项目 - 你使用的是针对x而不是y计算的button_size - 这将产生方形按钮,你永远不会使用任何类型的文本或按钮文件的描述。这就是说这段代码应该产生你想要的结果:

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