0

我正在构建一个由三个面板和 2 个网格(每个面板中有一个网格)组成的 GUI。我正在努力使用 BoxSizer 将对象放入各自的面板中。似乎垂直 boxsizer 只考虑了框架的上部和下部,而没有考虑到上部被分成两个面板。因此,我在尝试在上部垂直组织小部件时遇到问题。尝试将小部件添加到 panelTwo 时,垂直放置从 panelOne 拾取,反之亦然。如果可能,我想为每个面板分配一个垂直 boxsizer。请原谅草率的代码。我才刚接触 Python 几天,所以放轻松!我愿意接受建议!这是我遇到问题的部分:

    p1Sizer = wx.BoxSizer(wx.VERTICAL)
    p2Sizer = wx.BoxSizer(wx.VERTICAL)
    p3Sizer = wx.BoxSizer(wx.VERTICAL)`


    p3Sizer.Add(bottom_grid, 1, wx.EXPAND)#,pos=(200,200)

    p2Sizer.Add(p1Sizer)
    hbox1.Add(st1, flag=wx.ALL, border=5)
    hbox1.Add(tc1, flag=wx.ALL, border=10)
    hbox2.Add(st2, flag=wx.ALL, border=5)
    hbox2.Add(tc2, flag=wx.ALL, border=10)
    hbox3.Add(st3, flag=wx.ALL, border=5)
    hbox3.Add(tc3, flag=wx.ALL, border=5)
    p1Sizer.Add(hbox1,flag=wx.ALIGN_RIGHT)
    p1Sizer.Add(hbox2, flag=wx.ALIGN_RIGHT)
    p1Sizer.Add(hbox3, flag=wx.ALIGN_LEFT)

如果我包含此行,则 panelTwo 的格式正确。唯一的变化是底部框需要向下移动。见附件。

p2Sizer.Add(0,-120)

添加的行

如果我删除该行:

删除线

但我觉得这不是正确的方法。我还试图找到一种方法来创建两个具有不同行数的单独网格。IE

self.CreateGrid(100, 27)

和...

'self.CreateGrid(5, 27)'

import wx import wx.grid as gridlib import pandas as pd import numpy as np

class MyGrid(gridlib.Grid): def __init__(self, parent):

    """Constructor"""
    gridlib.Grid.__init__(self, parent)

    self.CreateGrid(100, 27)         

class MasterPanel(wx.Panel):

def __init__(self, parent, color):
    """Constructor"""
    wx.Panel.__init__(self, parent)
    self.SetBackgroundColour(color)`

class MainPanel(wx.Panel):

def __init__(self, parent):`

    """Constructor"""
    wx.Panel.__init__(self, parent)`

    topSplitter = wx.SplitterWindow(self)
    vSplitter = wx.SplitterWindow(topSplitter)
    font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)

    font.SetPointSize(50)

    panelOne = MasterPanel(vSplitter, "light grey")
    panelTwo = MasterPanel(vSplitter, "white")
    panelThree = MasterPanel(topSplitter,"white")

    vSplitter.SplitVertically(panelOne, panelTwo)
    vSplitter.SetMinimumPaneSize(300) # 300

    vSplitter.SetSashGravity(.5)


    topSplitter.SplitHorizontally(vSplitter, panelThree)
    topSplitter.SetMinimumPaneSize(200) #200
    topSplitter.SetSashGravity(1)
    #topSplitter.SetSashGravity(1)

# Assign grid to BOTTOM PANEL bottom_grid = MyGrid(panelThree) top_grid= MyGrid(panelTwo)

    st1 = wx.StaticText(panelOne, -1,"Name")
    st1.SetFont(font)
    st2 = wx.StaticText(panelOne, -1,"Date:")    
    st2.SetFont(font)
    st3 = wx.StaticText(panelOne, -1,"Input #:")
    st3.SetFont(font)
    st4 = wx.StaticText(panelTwo, -1,"From Source:")
    st4.SetFont(font)
    st5 = wx.StaticText(panelTwo, -1,"Lookup:")
    st5.SetFont(font)
    st6 = wx.StaticText(panelTwo, -1,"Lookup Value:")
    st6.SetFont(font)

    tc1 = wx.TextCtrl(panelOne,size=(150,25), value="-enter name-")
    tc2 = wx.TextCtrl(panelOne,size=(150,25), value="-enter date-")
    tc3 = wx.TextCtrl(panelOne, value="-input item #'")
    tc4 = wx.TextCtrl(panelTwo, value="-Search-")

    options= ['A','B','C']

    combo1 = wx.ComboBox(panelTwo,size=(150,25),value="-Choose Source-",choices=options, 
    style=wx.LB_SINGLE)
    combo2 = wx.ComboBox(panelTwo,size=(150,25),value="-Choose Lookup Type-",choices=options, 
    style=wx.LB_SINGLE)

    hbox1 = wx.BoxSizer(wx.HORIZONTAL)
    hbox2 = wx.BoxSizer(wx.HORIZONTAL)
    hbox3 = wx.BoxSizer(wx.HORIZONTAL)
    hbox4 = wx.BoxSizer(wx.HORIZONTAL)
    hbox5 = wx.BoxSizer(wx.HORIZONTAL)
    hbox6 = wx.BoxSizer(wx.HORIZONTAL)

    p1Sizer = wx.BoxSizer(wx.VERTICAL)
    p2Sizer = wx.BoxSizer(wx.VERTICAL)
    p3Sizer = wx.BoxSizer(wx.VERTICAL)


    p3Sizer.Add(bottom_grid, 1, wx.EXPAND)#,pos=(200,200)

    p2Sizer.Add(p1Sizer)
    hbox1.Add(st1, flag=wx.ALL, border=5)
    hbox1.Add(tc1, flag=wx.ALL, border=10)
    hbox2.Add(st2, flag=wx.ALL, border=5)
    hbox2.Add(tc2, flag=wx.ALL, border=10)
    hbox3.Add(st3, flag=wx.ALL, border=5)
    hbox3.Add(tc3, flag=wx.ALL, border=5)
    p1Sizer.Add(hbox1,flag=wx.ALIGN_RIGHT)
    p1Sizer.Add(hbox2, flag=wx.ALIGN_RIGHT)
    p1Sizer.Add(hbox3, flag=wx.ALIGN_LEFT)


    #p2Sizer.Add(0,-120)

    hbox4.Add(st4, flag=wx.ALL, border=5)
    hbox4.Add(combo1, flag=wx.ALL, border=5)
    hbox5.Add(st5, flag=wx.ALL, border=5)
    hbox5.Add(combo2, flag=wx.ALL, border=5)
    hbox6.Add(st6, flag=wx.ALL, border=5)
    hbox6.Add(tc4, flag=wx.ALL, border=5)
    p2Sizer.Add(hbox4)
    p2Sizer.Add(hbox5)
    p2Sizer.Add(hbox6)




    #p1Sizer.Add(p1, 2, wx.EXPAND)#,pos=(200,200)
    p2Sizer.Add(top_grid, 1, wx.EXPAND)#,pos=(200,200)


    panelThree.SetSizer(p3Sizer)
    panelTwo.SetSizer(p2Sizer)
    #panelOne.SetSizer(p1Sizer)

    sizer = wx.BoxSizer(wx.VERTICAL)

    #sizer.Add(top_grid)
    sizer.Add(topSplitter, -1, wx.EXPAND)

    #panelTwo.SetSizer(sizer)
    self.SetSizer(sizer)

class MainFrame(wx.Frame):

def __init__(self):
    """Constructor"""
    wx.Frame.__init__(self, None, title="Hell Yeah",
                      size=(1600,700))
    panel = MainPanel(self)
    self.Show()   `

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

4

1 回答 1

0

我已经“解释”了您的图像,转储了拆分窗口,这只会增加复杂性。我还转储了字体大小。
随意使用下面的代码作为起点,如果它们是必要的,请重新启用它们。

更新:在OP修改了问题的图片之后

这只是将布局划分为框的问题。在这种情况下,在问题图像中添加了额外的网格,只是需要一些更多的虚拟框。(我选择将 p2 拆分为 p21 和 p22,然后将它们放入 p2。这是正确对齐 3selection个小部件及其文本所必需的唯一原因)
我限制顶部网格的大小top_grid.SetMaxSize((-1,80))

import wx
import wx.grid as gridlib

class MyGrid(gridlib.Grid):
    def __init__(self, parent):
        """Constructor"""
        gridlib.Grid.__init__(self, parent)

        self.CreateGrid(100, 27)

class MasterPanel(wx.Panel):
    def __init__(self, parent, color):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour(color)

class MainPanel(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)

        panelOne = MasterPanel(self, "light grey")
        panelTwo = MasterPanel(self, "white")
        panelThree = MasterPanel(self,"white")

    # Assign grid to BOTTOM PANEL
        bottom_grid = MyGrid(panelThree)
        top_grid = MyGrid(panelTwo)
        top_grid.SetMaxSize((-1,80))

        st1 = wx.StaticText(panelOne, -1,"Name")
        st1.SetFont(font)
        st2 = wx.StaticText(panelOne, -1,"Date:")
        st2.SetFont(font)
        st3 = wx.StaticText(panelOne, -1,"Input #:")
        st3.SetFont(font)
        st4 = wx.StaticText(panelTwo, -1,"From Source:")
        st4.SetFont(font)
        st5 = wx.StaticText(panelTwo, -1,"Lookup:")
        st5.SetFont(font)
        st6 = wx.StaticText(panelTwo, -1,"Lookup Value:")
        st6.SetFont(font)

        tc1 = wx.TextCtrl(panelOne, value="-enter name-")
        tc2 = wx.TextCtrl(panelOne, value="-enter date-")
        tc3 = wx.TextCtrl(panelOne, value="-input item #'")
        tc4 = wx.TextCtrl(panelTwo, value="-Search-")

        options= ['Option 1','Option 2','Option 3']

        combo1 = wx.ComboBox(panelTwo,value="-Choose Source-",choices=options,
        style=wx.LB_SINGLE)
        combo2 = wx.ComboBox(panelTwo,value="-Choose Lookup Type-",choices=options,
        style=wx.LB_SINGLE)

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        hbox4 = wx.BoxSizer(wx.HORIZONTAL)
        hbox5 = wx.BoxSizer(wx.HORIZONTAL)
        hbox6 = wx.BoxSizer(wx.HORIZONTAL)
        hbox7 = wx.BoxSizer(wx.HORIZONTAL)

        p1Sizer = wx.BoxSizer(wx.VERTICAL)
        p2Sizer = wx.BoxSizer(wx.VERTICAL)
        p21Sizer = wx.BoxSizer(wx.VERTICAL)
        p22Sizer = wx.BoxSizer(wx.VERTICAL)
        p3Sizer = wx.BoxSizer(wx.VERTICAL)
        p4Sizer = wx.BoxSizer(wx.VERTICAL)
        p3Sizer.Add(bottom_grid, 1, wx.EXPAND)

        hbox1.Add(st1, flag=wx.ALL, border=5)
        hbox1.Add(tc1, flag=wx.ALL, border=5)
        hbox2.Add(st2, flag=wx.ALL, border=5)
        hbox2.Add(tc2, flag=wx.ALL, border=5)
        hbox3.Add(st3, flag=wx.ALL, border=5)
        hbox3.Add(tc3, flag=wx.ALL, border=5)
        p1Sizer.Add(hbox1, flag=wx.ALIGN_RIGHT)
        p1Sizer.Add(hbox2, flag=wx.ALIGN_RIGHT)
        p1Sizer.Add(hbox3, flag=wx.ALIGN_RIGHT)

        hbox4.Add(st4, flag=wx.ALL, border=5)
        hbox4.Add(combo1, flag=wx.ALL, border=5)
        hbox5.Add(st5, flag=wx.ALL, border=5)
        hbox5.Add(combo2, flag=wx.ALL, border=5)
        hbox6.Add(st6, flag=wx.ALL, border=5)
        hbox6.Add(tc4, flag=wx.ALL, border=5)
        hbox7.Add(top_grid, flag=wx.ALL, border=5)
        p21Sizer.Add(hbox4, flag=wx.ALIGN_RIGHT)
        p21Sizer.Add(hbox5, flag=wx.ALIGN_RIGHT)
        p21Sizer.Add(hbox6, flag=wx.ALIGN_RIGHT)
        p22Sizer.Add(hbox7)

        p2Sizer.Add(p21Sizer)
        p2Sizer.Add(p22Sizer)

        panelThree.SetSizer(p3Sizer)
        panelTwo.SetSizer(p2Sizer)
        panelOne.SetSizer(p1Sizer)

        mainsizer_top = wx.BoxSizer(wx.HORIZONTAL)
        mainsizer_bottom = wx.BoxSizer(wx.VERTICAL)
        mainsizer = wx.BoxSizer(wx.VERTICAL)

        mainsizer_top.Add(panelOne, 0, wx.EXPAND)
        mainsizer_top.Add(panelTwo, 0,)
        mainsizer_bottom.Add(panelThree, 1, wx.EXPAND)
        mainsizer.Add(mainsizer_top, 0,)
        mainsizer.Add(mainsizer_bottom, 1, wx.EXPAND)

        self.SetSizer(mainsizer)

class MainFrame(wx.Frame):
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Hell Yeah",
                          size=(1600,700))
        panel = MainPanel(self)
        self.Show()

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

在此处输入图像描述

于 2020-05-11T19:30:10.177 回答