使用boxsizers

时间:2016-07-25 09:55:28

标签: python python-2.7 wxpython

我最近开始使用wxPython来构建GUI,并且我尝试创建以下布局:

Button1 Button2 Button3
----------------------------------------
listbox | textctrl

按钮应具有灵活的宽度,扩展以填充框架的整个宽度,并在它们之间有边框(每个按钮的宽度(包括边框)为1/3框架)。它们的高度应设置为高度(以像素为单位)。

列表框应垂直填充框架,并设置宽度为x像素

textctrol应该是一个文本框,可以展开以垂直和水平填充框架的宽度。

这是我的代码:

mainPanel = wx.Panel(self, -1)
parentBox = wx.BoxSizer(wx.VERTICAL)

menubar = wx.MenuBar()
filem = wx.Menu()

menubar.Append(filem, '&File')
self.SetMenuBar(menubar)

navPanel = wx.Panel(mainPanel, -1, size=(1000, 80))
navBox = wx.BoxSizer(wx.HORIZONTAL)

newSection = wx.Button(navPanel, self.ID_NEW, 'New')
renSection = wx.Button(navPanel, self.ID_RENAME, 'Rename')
dltSection = wx.Button(navPanel, self.ID_DELETE, 'Delete')

navBox.Add(newSection, 1, wx.EXPAND | wx.ALL, 5)
navBox.Add(renSection, 1, wx.EXPAND | wx.ALL, 5)
navBox.Add(dltSection, 1, wx.EXPAND | wx.ALL, 5)          

navPanel.SetSizer(navBox)

contentPanel = wx.Panel(mainPanel, -1, size=(1000, 600))
contentBox = wx.BoxSizer(wx.HORIZONTAL)

self.listbox = wx.ListBox(contentPanel, -1, size=(300, 700))
self.settings = wx.TextCtrl(contentPanel, -1)

contentBox.Add(self.listbox, 0)
contentBox.Add(self.settings, 1, wx.EXPAND | wx.ALL, 5)

contentPanel.SetSizer(contentBox)

parentBox.Add(navPanel, 0, wx.EXPAND | wx.ALL, 5)
parentBox.Add(contentPanel, 1, wx.EXPAND | wx.ALL, 5)

mainPanel.SetSizer(parentBox)

出现问题,因为我看到的不是我期望看到的,任何可以帮助我的人?​​

1 个答案:

答案 0 :(得分:0)

这对我有用,我在win64,python 32bit 2.7.3.3,wx' 2.8.12.1(msw-unicode)'。完整的工作测试示例是:

import wx

class testframe(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'some title')
        mainPanel = wx.Panel(self, -1)
        parentBox = wx.BoxSizer(wx.VERTICAL)

        menubar = wx.MenuBar()
        filem = wx.Menu()

        menubar.Append(filem, '&File')
        self.SetMenuBar(menubar)

        navPanel = wx.Panel(mainPanel, -1, size=(1000, 80))
        navBox = wx.BoxSizer(wx.HORIZONTAL)

        newSection = wx.Button(navPanel, -1, 'New')
        renSection = wx.Button(navPanel, -1, 'Rename')
        dltSection = wx.Button(navPanel, -1, 'Delete')

        navBox.Add(newSection, 1, wx.EXPAND | wx.ALL, 5)
        navBox.Add(renSection, 1, wx.EXPAND | wx.ALL, 5)
        navBox.Add(dltSection, 1, wx.EXPAND | wx.ALL, 5)          

        navPanel.SetSizer(navBox)

        contentPanel = wx.Panel(mainPanel, -1, size=(1000, 600))
        contentBox = wx.BoxSizer(wx.HORIZONTAL)

        self.listbox = wx.ListBox(contentPanel, -1, size=(300, 700))
        self.settings = wx.TextCtrl(contentPanel, -1)

        contentBox.Add(self.listbox, 0, wx.ALL, 5)
        contentBox.Add(self.settings, 1, wx.EXPAND | wx.ALL, 5)

        contentPanel.SetSizer(contentBox)

        parentBox.Add(navPanel, 0, wx.EXPAND | wx.ALL, 5)
        parentBox.Add(contentPanel, 1, wx.EXPAND | wx.ALL, 5)

        mainPanel.SetSizer(parentBox)
        parentBox.Fit(self)

app = wx.PySimpleApp()
app.frame = testframe()
app.frame.Show()
app.MainLoop()

注意添加主{s}的Fit(),以及添加到列表框中的5px边框。