在 wxPython 中自动排列面板 [英] Arranging the panels automatically in wxPython

查看:30
本文介绍了在 wxPython 中自动排列面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

级别:初级

我正在 Windows 操作系统上使用 wxPython 开发 GUI.我在安排面板时遇到了一些问题.我的图形界面如下图所示(当然图中省略了菜单栏和标题栏).

I am developing a GUI with wxPython on Windows OS. I have some problems with arranging the panels. I GUI looks like below image (Of course the menu bar and the title bar is omitted in the image).

我会花一些时间来解释我是如何创建这个 GUI 的.首先,我使用 wx.DisplaySize() 获取屏幕尺寸.然后我使用 pos=() 相应地调整我的面板.有主要的 4 个面板,名为 panel-1 到 panel-4.每个面板都包含子面板.子面板的数量不是常数.我使用 for 循环来创建子面板并将其添加到面板.子面板被添加到一个sizer,然后sizer最终被应用到相应的面板上.根据 wx.DisplaySize() 我的屏幕尺寸是 1680x1050.目前一切正常.

I would take some time to explain how I create this GUI. First I get the screen size with the wx.DisplaySize(). Then I adjust my panels accordingly using pos=(). There are main 4 panels named panel-1 to panel-4. Each of this panels contains subpanels. The number of subpanels is not a contstant. I use a for loop to create and add subpanels to panels. The subpanels are added to a sizer and then the sizers are ultimately applied to the corresponding panels. My screen size according to wx.DisplaySize() is 1680x1050. Currently everything is working good.

问题:

  1. 但是一旦我在屏幕尺寸为 1366x768 的机器上运行我的代码,面板就没有正确定位!如何让我的 GUI 根据屏幕尺寸的变化自动调整面板的排列.我计划在从平板电脑到大屏幕 LCD 的各种屏幕尺寸上使用我的 GUI.我想我必须创建一个 sizer 将这些名为 panel-1 的面板添加到 panel4 到某个主任意面板?或者有什么更好的方法?

  1. But as soon as I run my code on a machine with screen size 1366x768 the panels are not positioned correctly! How can I make my GUI to auto adjust the panel's arrangement according to the screen size change. I plan to use my GUI on a variety of screen sizes from tablets to large screen LCDs. I guess I have to create a sizer that add these panels named as panel-1 to panel4 to some main arbitrary panel? Or is there any better approach?

目前我已经禁用了框架的调整大小选项.因为当我调整框架大小(启用选项时)时,主窗口会调整大小,但主窗口内的组件(面板、按钮)不会调整大小.如何启用此功能,以便在调整主窗口大小时,内部组件也会相应调整大小?

Currently I am have disabled the resize option of the frame. Because when I resize the frame (when option is enabled) the main window gets resized but the components (panels, buttons) inside the main windows don't get resized. How can I enable this feature so that when the main window is resized the components inside are also resized accordingly?

我在创建此类 GUI 的方法中可能存在错误.我期待任何可以以简化方式创建此类 GUI 的建议!感谢您抽出宝贵时间.

There maybe an error in my approach in creating this kind of GUI. I look forward any suggestions that can create this kind of GUI in a simplified way! Thanks for your time.

PS:如果需要,我可以粘贴代码,但我认为这并不重要,因为它只会增加帖子长度,而且我已经尝试详细解释我的问题.但是,我的代码适用于 1680x1050 的屏幕尺寸.

PS: I can paste the code if required but I don't think it is important as it will just increase the post length and as I have tried to explain my problem in quite a detail. However my code is working good for the screen size 1680x1050.

推荐答案

您不能在不同的屏幕尺寸上使用绝对定位.wxPython 工具包提供了可帮助您解决此问题的 sizer.如果您的所有小部件都在 sizers 内,它们将自动适当地调整大小.这是一种与您的设计非常相似的实现:

You cannot use absolute positioning across different screen sizes. The wxPython toolkit provides sizers that will help you solve this problem. If all your widgets are within sizers, they will automatically resize appropriately. Here's one implementation that is very similar to your design:

import wx

########################################################################
class SubPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent, number):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour("red")

        label = "Sub panel-%s" % number
        lbl = wx.StaticText(self, label=label)

        sizer = wx.BoxSizer()
        sizer.Add(lbl, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)

########################################################################
class ColorPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent, number, color, sub_panels):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour(color)

        label = "Panel-%s" % number
        lbl = wx.StaticText(self, label=label)

        v_sizer = wx.BoxSizer(wx.VERTICAL)
        for i in range(sub_panels):
            p = SubPanel(self, i+1)
            v_sizer.Add(p, 0, wx.ALL|wx.EXPAND|wx.CENTER, 10)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(v_sizer, 0, wx.ALL, 5)
        sizer.Add(lbl, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)

########################################################################
class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        v_sizer = wx.BoxSizer(wx.VERTICAL)

        colors = [("green", 3),
                  ("yellow", 2),
                  ("light blue", 2),
                  ("purple", 2)]
        count = 1
        for color, subpanel in colors:
            panel = ColorPanel(self, count, color, subpanel)
            hsizer.Add(panel, 1, wx.EXPAND)
            count += 1

        orange_panel = ColorPanel(self, count, "orange", 0)
        v_sizer.Add(hsizer, 1, wx.EXPAND)
        v_sizer.Add(orange_panel, 1, wx.EXPAND)

        self.SetSizer(v_sizer)

########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Panels!", size=(600,400))
        panel = MainPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

这篇关于在 wxPython 中自动排列面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆