在顶部和中心放置一个盒子

Place a box on the top and in the center

我正在尝试在网格中放置四个蓝色框。首先,我想从中间和顶部的一个蓝色框开始。不幸的是,我什至想不出一个盒子来正确放置它。 我试过制作两个盒子和一个面板。我已经尝试使用静态文本并且它起作用了,所以我想用盒子来适应这个原则,但我不知道我做错了什么。你能看看我做错了什么吗?

我想达到的效果是这样的:

下面是我的代码:

    import wx
class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        panel1 = self.panels()
        self.boxes(panel1)

    def panels(self):
        panel1 = wx.Panel(self)
        panel1.SetBackgroundColour([0, 178, 202])
        return panel1

    def boxes(self,panel1):
        hbluebox1 = wx.BoxSizer()
        vbluebox1 = wx.BoxSizer(wx.VERTICAL)

        hbluebox1.Add(panel1, 6, wx.ALIGN_TOP)
        vbluebox1.Add(hbluebox1, 6, wx.ALIGN_CENTRE)
        self.SetSizer(vbluebox1)
        return vbluebox1

if __name__ == '__main__':
    class Screen(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, size=(600, 400))
            framebox = wx.BoxSizer()
            panel = Panel(self)
            framebox.Add(panel, 1, wx.EXPAND | wx.ALL)
            self.SetSizer(framebox)
            self.Show()
    app = wx.App()
    Screen(None, -1, "Kleuruitdager 2")
    app.MainLoop()

您可以使用简单的彩色面板和 wx.GridBagSizer

我会让你完成调整和中央面板(可能以类似的方式实现)。
另一种选择当然是按照评论中的建议开始绘图。

import wx

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        sizer = wx.GridBagSizer(3, 3)
        top_lpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        top_mpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        top_rpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        top_lpanel.SetBackgroundColour("white")
        top_mpanel.SetBackgroundColour("blue")
        top_rpanel.SetBackgroundColour("white")
        
        mid_lpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        mid_mpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        mid_rpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        mid_lpanel.SetBackgroundColour("blue")
        mid_mpanel.SetBackgroundColour("white")
        mid_rpanel.SetBackgroundColour("blue")
        
        bot_lpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        bot_mpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        bot_rpanel = wx.Panel(self, wx.ID_ANY, size=(100, 100))
        bot_lpanel.SetBackgroundColour("white")
        bot_mpanel.SetBackgroundColour("blue")
        bot_rpanel.SetBackgroundColour("white")
        
        sizer.Add(top_lpanel, pos=(0, 0), flag=wx.EXPAND, border = 10)
        sizer.Add(top_mpanel, pos=(0, 1), flag=wx.EXPAND, border = 10)
        sizer.Add(top_rpanel, pos=(0, 2), flag=wx.EXPAND, border = 10)
        sizer.Add(mid_lpanel, pos=(1, 0), flag=wx.EXPAND, border = 10)
        sizer.Add(mid_mpanel, pos=(1, 1), flag=wx.EXPAND, border = 10)
        sizer.Add(mid_rpanel, pos=(1, 2), flag=wx.EXPAND, border = 10)
        sizer.Add(bot_lpanel, pos=(2, 0), flag=wx.EXPAND, border = 10)
        sizer.Add(bot_mpanel, pos=(2, 1), flag=wx.EXPAND, border = 10)
        sizer.Add(bot_rpanel, pos=(2, 2), flag=wx.EXPAND, border = 10)                
        self.SetSizer(sizer)

if __name__ == '__main__':
    class Scherm(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent)
            framebox = wx.BoxSizer()
            panel = Panel(self)
            framebox.Add(panel, 1, wx.EXPAND | wx.ALL)
            self.SetSizerAndFit(framebox)
            self.Show()
    
    app = wx.App()
    Scherm(None)
    app.MainLoop()

感谢您的帮助,今天我终于想通了如何去做。现在我明白了如何去做的原则。 Endresult

import wx


class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        #blue and non-colored boxes
        left_panel1 = self.box1()
        mid_panel1, white_box = self.box2()
        right_panel1 = self.box3()
        #yellow_boxes
        mid_yellow_panel1 = self.yellow_top1()
        mid_yellow_panel2 = self.yellow_top2()
        mid_yellow_panel3 = self.yellow_top3()
        #box for the yellow boxes
        self.yellow_boxes(white_box, mid_yellow_panel1, mid_yellow_panel2, mid_yellow_panel3)
        #The mainbox
        main_box = self.boxes(left_panel1, mid_panel1, right_panel1)
        self.SetSizer(main_box)

    def box1(self):
        left_panel1 = wx.BoxSizer(wx.VERTICAL)
        # 1e witte box
        white_top1 = wx.Panel(self)
        # 1e witte box in de grote box
        left_panel1.Add(white_top1, 7, wx.EXPAND | wx.ALL)

        # 1e blauwe box
        blue_top1 = wx.Panel(self)
        blue_top1.SetBackgroundColour([0, 178, 202])
        # Voegt 1e blauwe box toe in de grote box
        left_panel1.Add(blue_top1, 6, wx.EXPAND | wx.ALL)

        # 2e witte box
        white_bottom1 = wx.Panel(self)
        # 2e witte box in de grote box
        left_panel1.Add(white_bottom1, 7, wx.EXPAND | wx.ALL)
        return left_panel1

    def box2(self):
        mid_panel1 = wx.BoxSizer(wx.VERTICAL)
        # 1e blauwe box
        blue_top1 = wx.Panel(self)
        blue_top1.SetBackgroundColour([0, 178, 202])
        # Voegt 1e blauwe box toe in de grote box
        mid_panel1.Add(blue_top1, 6, wx.EXPAND | wx.ALL)

        # 1e witte box
        white_box = wx.BoxSizer()
        # 1e witte box in de grote box
        mid_panel1.Add(white_box, 7, wx.EXPAND | wx.ALL)

        # 2e blauwe box
        blue_top1 = wx.Panel(self)
        blue_top1.SetBackgroundColour([0, 178, 202])
        # Voegt 1e blauwe box toe in de grote box
        mid_panel1.Add(blue_top1, 6, wx.EXPAND | wx.ALL)

        return mid_panel1, white_box

    def box3(self):
        right_panel1 = wx.BoxSizer(wx.VERTICAL)
        # 1e witte box
        white_top1 = wx.Panel(self)
        # 1e witte box in de grote box
        right_panel1.Add(white_top1, 7, wx.EXPAND | wx.ALL)

        # 1e blauwe box
        blue_top1 = wx.Panel(self)
        blue_top1.SetBackgroundColour([0, 178, 202])
        # Voegt 1e blauwe box toe in de grote box
        right_panel1.Add(blue_top1, 6, wx.EXPAND | wx.ALL)

        # 2e witte box
        white_bottom1 = wx.Panel(self)
        # 2e witte box in de grote box
        right_panel1.Add(white_bottom1, 7, wx.EXPAND | wx.ALL)
        return right_panel1

    #Yellow panels
    def yellow_top1(self):
        mid_yellow_panel1 = wx.BoxSizer(wx.VERTICAL)
        # 1e witte box
        white_top1 = wx.Panel(self)
        # 1e witte box in de grote box
        mid_yellow_panel1.Add(white_top1, 3, wx.EXPAND | wx.ALL)

        # 1e gele box
        yellow_top = wx.Panel(self)
        yellow_top.SetBackgroundColour(wx.YELLOW)
        # Voegt 1e gele box toe in de grote box
        mid_yellow_panel1.Add(yellow_top, 2, wx.EXPAND | wx.ALL)

        # 2e witte box
        white_bottom1 = wx.Panel(self)
        # 2e witte box in de grote box
        mid_yellow_panel1.Add(white_bottom1, 3, wx.EXPAND | wx.ALL)
        return mid_yellow_panel1

    def yellow_top2(self):
        mid_yellow_panel2 = wx.BoxSizer(wx.VERTICAL)
        # 1e gele box
        yellow_top = wx.Panel(self)
        yellow_top.SetBackgroundColour(wx.YELLOW)
        # Voegt 1e gele box toe in de grote box
        mid_yellow_panel2.Add(yellow_top, 2, wx.EXPAND | wx.ALL)

        # 1e witte box
        white_top1 = wx.Panel(self)
        # 1e witte box in de grote box
        mid_yellow_panel2.Add(white_top1, 4, wx.EXPAND | wx.ALL)

        # 2e gele box
        yellow_top = wx.Panel(self)
        yellow_top.SetBackgroundColour(wx.YELLOW)
        # Voegt 1e gele box toe in de grote box
        mid_yellow_panel2.Add(yellow_top, 2, wx.EXPAND | wx.ALL)

        return mid_yellow_panel2

    def yellow_top3(self):
        mid_yellow_panel3 = wx.BoxSizer(wx.VERTICAL)
        # 1e witte box
        white_top1 = wx.Panel(self)
        # 1e witte box in de grote box
        mid_yellow_panel3.Add(white_top1, 3, wx.EXPAND | wx.ALL)

        # 1e gele box
        yellow_top = wx.Panel(self)
        yellow_top.SetBackgroundColour(wx.YELLOW)
        # Voegt 1e gele box toe in de grote box
        mid_yellow_panel3.Add(yellow_top, 2, wx.EXPAND | wx.ALL)

        # 2e witte box
        white_bottom1 = wx.Panel(self)
        # 2e witte box in de grote box
        mid_yellow_panel3.Add(white_bottom1, 3, wx.EXPAND | wx.ALL)
        return mid_yellow_panel3

    def yellow_boxes(self, white_box, mid_yellow_panel1, mid_yellow_panel2, mid_yellow_panel3):
        white_box.Add(mid_yellow_panel1, 8, wx.EXPAND | wx.ALL)
        white_box.Add(mid_yellow_panel2, 8, wx.EXPAND | wx.ALL)
        white_box.Add(mid_yellow_panel3, 8, wx.EXPAND | wx.ALL)
        return white_box

    def boxes(self, left_panel1, mid_panel1, right_panel1):
        main_box = wx.BoxSizer()
        main_box.Add(left_panel1, 20, wx.EXPAND | wx.ALL)
        main_box.Add(mid_panel1, 20, wx.EXPAND | wx.ALL)
        main_box.Add(right_panel1, 20, wx.EXPAND | wx.ALL)
        return main_box



if __name__ == '__main__':
    class Scherm(wx.Frame):
        def __init__(self, parent, size1):
            wx.Frame.__init__(self, parent, size = size1)
            framebox = wx.BoxSizer()
            panel = Panel(self)
            framebox.Add(panel, 1, wx.EXPAND | wx.ALL)
            self.SetSizer(framebox)
            self.Show()


    app = wx.App()
    Scherm(None, (600, 600))
    app.MainLoop()