1

上部にタブがあり、下部に一般的なパネルがあるwx.Notebookを使用してプロジェクトツールを作成しようとしています。下部のパネルはノートブックから独立している必要があり、タブが変更されても変更されません。ノートブックを追加するだけではノートブック自体は正常に機能しますが、下部パネル(拡張wx.Panel)も追加すると、ノートブックは次の画像に示すように圧縮されます

ウィンドウ用のパネルが1つあり、ノートブックと下部パネル(BottomGroupと呼ばれ、wx.Panelを拡張)を別々のパネルに追加します。誰かが問題を見つけることができますか?多分サイザーで何か?私のウィンドウは次のようになります(タブが間違っていることに注意してください):

class Window(wx.Frame):

def __init__(self, parent, title):

    wx.Frame.__init__(self, None, wx.ID_ANY, title)
    self.InitUI()


def InitUI(self):    

    menuBar = wx.MenuBar()
    menu = wx.Menu()
    menu_load = menu.Append(wx.ID_OPEN, 'Open', 'Open project')
    menu_save = menu.Append(wx.ID_SAVE, 'Save', 'Save project')
    menu_save_as = menu.Append(wx.ID_SAVEAS, 'Save as', 'Save project as')
    menu_exit = menu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
    menuBar.Append(menu, '&File')
    self.SetMenuBar(menuBar)

    mainPanel = wx.Panel(self)

    self.noteBookPanel = wx.Panel(mainPanel)
    self.notebook = wx.Notebook(self.noteBookPanel)
    self.bottomPanel = wx.Panel(mainPanel)
    self.bottomGroup = BottomGroup(self.bottomPanel)

    mainSizer = wx.BoxSizer(wx.VERTICAL)

    self.pageNodePathsTables = PageNodesPathsTables(self.notebook)
    self.pageEscapeCriteria = PageEscapeCriteria(self.notebook)
    self.pageFileHandling = PageFileHandling(self.notebook)

    self.notebook.AddPage(self.pageNodePathsTables, "Define Paths and Nodes")
    self.notebook.AddPage(self.pageEscapeCriteria, "Define Escape Criteria")
    self.notebook.AddPage(self.pageFileHandling, "File Handling")

    mainSizer.Add(self.noteBookPanel,1,wx.TOP)
    mainSizer.Add(self.bottomGroup,1,wx.BOTTOM)

    self.Bind(wx.EVT_MENU, self.onSave, menu_save)
    self.Bind(wx.EVT_MENU, self.onLoad, menu_load)
    self.Bind(wx.EVT_MENU, self.OnQuit, menu_exit)

    self.SetDimensions(WindowOpenX,WindowOpenY,WindowWidth,WindowHeight) 
    self.Show(True)

アップデート:

私は自分のコードをこれにリファクタリングしました(リファクタリングされた部分のみが示されています):

    self.notebook = wx.Notebook(self)
    self.bottomGroup = BottomGroup(self)

    self.setupMenu()

    #frameSizer = wx.GridBagSizer(rowGap,columnGap)
    #frameSizer.Add(self.notebook,pos=(0,0), span=(1,1),
    #              flag=wx.LEFT|wx.TOP|wx.EXPAND, border = 5)
    #frameSizer.Add(self.bottomGroup,pos=(1,0), span=(1,1),
    #              flag=wx.LEFT|wx.BOTTOM|wx.EXPAND, border = 5)

    frameSizer = wx.BoxSizer(wx.VERTICAL)
    frameSizer.Add(self.notebook, 2, wx.EXPAND)
    frameSizer.Add(self.bottomGroup,0)
    self.SetSizer(frameSizer)

ここで、self.setupMenu()は次のように定義されます。

def setupMenu(self):
    self.pageNodePathsTables = PageNodesPathsTables(self.notebook)
    self.pageEscapeCriteria = PageEscapeCriteria(self.notebook)
    self.pageFileHandling = PageFileHandling(self.notebook)
    self.pagePlotHistoryData = PagePlotHistoryData(self.notebook)
    self.pageCalculateEscape = PageCalculateEscape(self.notebook)

    self.notebook.AddPage(self.pageNodePathsTables, "Define Paths and Nodes")
    self.notebook.AddPage(self.pageEscapeCriteria, "Define Escape Criteria")
    self.notebook.AddPage(self.pageFileHandling, "File Handling")
    self.notebook.AddPage(self.pagePlotHistoryData, "Plot History Data")
    self.notebook.AddPage(self.pageCalculateEscape, "Calculate Escape")

これは、上記のコードよりもはるかに明確で簡単です。bottomGroupがスタックされていることを除いて、正常に機能しますnotebook(つまり、両方の要素がwx.Frameの左上隅から始まります)。私はとの両方を試しましwx.BoxSizerwx.GridBagLayout(上記でコメントしたように)。この問題について何か提案はありますか?

私のBottomGroupは次のように定義されています:

class BottomGroup(wx.Panel):

def __init__(self,parent):
    wx.Panel.__init__(self,parent)
    panelSizer = wx.GridBagSizer(rowGap,columnGap)

    btnSaveProject = wx.Button(parent, label="Save project", size=(100,50))
    btnLoadProject = wx.Button(parent, label="Open project", size=(100,50))

    panelSizer.Add(btnSaveProject, pos=(0,0), span=(1,1),
                             flag=wx.EXPAND|wx.LEFT, border = borderWidth)
    panelSizer.Add(btnLoadProject, pos=(0,1), span=(1,1),
                             flag=wx.EXPAND|wx.LEFT, border = borderWidth)

    self.SetSizer(panelSizer)

私の主な方法は次のようなものです。

if __name__ == '__main__':
    app = wx.App()
    Window(None, WindowHeader)
    app.MainLoop()
4

1 に答える 1

1

I don't want to provide you with a full solution, but rather a little advice on UI programming.

You screwed up by creating to many panels and never setting a sizer. Before writing UI code always think of the structure (hierarchy) of widgets you want to create. Think of their layout. Then write down the code, grouped by principle of locality: approximately one level of the hierarchy should be handled in a few lines.

Good, let's apply the principle. Notebook and bottom panel at top level:

wx.Frame  
 +- wx.NoteBook
     +- PageNodesPathsTables
     +- ...
 +- BottomGroup

At this level, everything comes down to these four, simple lines:

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 2, wx.EXPAND)
sizer.Add(self.bottomPanel, 0)
self.SetSizer(sizer)

Obviously you know how to handle a wx.NoteBook properly. Very important is the call to self.SetSizer (non-existent in your code), because otherwise the frame has no idea which sizer it should use for layouting its children.
You demands about the partitioning in two halves where pretty clear. Words of wisdom: Don't do more nesting than needed. This might be more extensible (and can easily be postponed), but at the moment it's not needed. If you do something like this:

self.bottomPanel = wx.Panel(mainPanel)
self.bottomGroup = BottomGroup(self.bottomPanel)

You have introduced at least one level (self.bottomPanel) with a single child (self.bottomGroup) that has no clue how to layout its child components. Erase the unnecessary nesting, and you will get rid of panel creation, sizer creation, sizer assignment, and so on. Collapsing it will get you a quicker overview. Todo:

  • Rewrite this section of you program. Get rid of superfluous instructions.
  • Make sure every panel has a sizer.
  • Refactor menu creation in a separate method.
  • Parameter parent in the Window constructor is unused. Fix.
  • Your example was missing a main method, imports, etc. Next time, supply an SSCCE.
于 2013-01-25T10:16:48.670 に答える