【问题标题】:Python/Pygame: How to make buttons scale on screen with amount of buttons and screen size?Python/Pygame:如何使按钮在屏幕上随按钮数量和屏幕尺寸缩放?
【发布时间】:2014-07-21 08:59:30
【问题描述】:

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

如您所见,我仍然是编程方面的业余爱好者,因此对于理解上面编写的代码可能有多么困难,我深表歉意。如果我需要澄清任何事情或回答任何问题,请告诉我,我会尽力回答!

提前致谢!

~大敲

【问题讨论】:

  • 首先 butmax 永远不会计算 - 只是比较,然后您对 x 轴的计算是使用按钮总数而不是适合的实际数字。基本上你的逻辑很遥远——你需要重新考虑计算——方法是正确的。

标签: python button user-interface pygame launcher


【解决方案1】:

我已尝试尽可能多地重用您的代码,但您可能想要解决几个大项目 - 您使用的是为 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()    

【讨论】:

  • 非常感谢你帮助我发现了这样一个任务背后的逻辑——它几乎完美地工作,我只需要调整代码的几个不同部分就可以让它按照我最初的预期工作。不仅如此,您还让我在自己的代码中出错的地方非常明显。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多