wxPython Notebook页面

时间:2015-05-28 22:45:00

标签: python wxpython

我正在尝试将代码添加到笔记本页面。它在一个框架中工作正常,但在从Panel派生的笔记本页面中却没有。我需要一些帮助理解面板并帮助修复我的代码。以下是笔记本页面的代码:

class PageOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        vbox_left = wx.BoxSizer(wx.VERTICAL)
        vbox_right = wx.BoxSizer(wx.VERTICAL)

        panel = wx.Panel(self, -1)
        hbox = wx.BoxSizer(wx.HORIZONTAL)

        driveList=["D:/", "E:/"]

        font = wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL)

        label = wx.StaticText(panel, -1, "Audio Source") # Add type/name of source
        label.SetFont(font)
        label.SetSize(label.GetBestSize())
        self.combo1 = wx.ComboBox(panel, style=wx.CB_DROPDOWN, choices=driveList)
        self.listbox = wx.ListBox(panel, 1, size=(380, 220))
        vbox_left.Add(label, 0, wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER_HORIZONTAL, 5)
        vbox_left.Add(self.combo1, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.LEFT | wx.RIGHT, 15)
        vbox_left.Add(self.listbox, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 15)

        label = wx.StaticText(panel, -1, "Local Disk") # Add disk name
        label.SetFont(font)
        label.SetSize(label.GetBestSize())
        self.combo2 = wx.ComboBox(panel, style=wx.CB_DROPDOWN, choices=driveList)
        self.listbox = wx.ListBox(panel, 0, size=(380, 220))
        vbox_right.Add(label, 0, wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER_HORIZONTAL, 5)
        vbox_right.Add(self.combo2, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.LEFT | wx.RIGHT, 15)       
        vbox_right.Add(self.listbox, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 15)

        hbox.Add(vbox_left, 0, wx.EXPAND | wx.LEFT, 20)
        hbox.Add(vbox_right, 0, wx.EXPAND | wx.RIGHT, 20)
        panel.SetSizer(hbox)
        panel.Layout()

如何更改它以显示在笔记本页面上它现在只是空白。

我有第二个notebok页面,其代码是:

class PageTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        panel = wx.Panel(self, -1)

        font = wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL)

        vbox = wx.BoxSizer(wx.VERTICAL)

        label = wx.StaticText(panel, -1, "Audio Source") # Add type/name of source
        label.SetFont(font)
        label.SetSize(label.GetBestSize())

        vbox.Add(label, 0, wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER_HORIZONTAL, 5)

对于那个,我只是在右上角看到一个带有截止u的A,如我设置的文字" Audio Source"。我不明白在这些笔记本页面上的定位。如果有人可以给我一个面板和笔记本页面定位的快速解释,并帮助我改变我的代码在笔记本页面工作,这将是一个巨大的帮助。但主要是我想了解为什么我的代码不起作用。我到目前为止已经放了两个小时而且卡住了。

哦,生成笔记本的代码:

    nb = wx.Notebook(panel)

    # create the page windows as children of the notebook
    page1 = PageOne(nb)
    page2 = PageTwo(nb)
    page3 = PageThree(nb)

    # add the pages to the notebook with the label to show on the tab
    nb.AddPage(page1, "Page 1")
    nb.AddPage(page2, "Page 2")
    nb.AddPage(page3, "Page 3")

    hbox = wx.BoxSizer(wx.HORIZONTAL)
    hbox.Add(nb, 1, wx.EXPAND)
    hbox.Add(btnPanel, 0.6, wx.EXPAND | wx.RIGHT, 20)
    btnPanel.SetSizer(vbox)
    panel.SetSizer(hbox)
    panel.Layout() 

由于

编辑:我能够在笔记本中添加一个对话框并使其正常工作。但我不能让它作为一个小组工作。我想将PageOne中的代码添加到笔记本页面中,但我得到的只是一个按钮,在选项卡页面的右上角闪烁。如何将项目添加到笔记本页面?我找到了另一个例子,他们使用wx.SplitterWindow,然后他们创建了两个网格。但是,我似乎无法弄清楚如何将内容放入页面,例如列表框,树形图等。正确的方法是什么?

以下是拆分器的工作代码:

import wx
import wx.grid as gridlib

class RegularPanel(wx.Panel):
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent)
    self.SetBackgroundColour("pink")

class GridPanel(wx.Panel):
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent)
    self.grid = gridlib.Grid(self, style=wx.BORDER_SUNKEN)
    self.grid.CreateGrid(25,8)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.grid, 1, wx.EXPAND)
    self.SetSizer(sizer)

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

    notebook = wx.Notebook(self)

    page = wx.SplitterWindow(notebook)
    notebook.AddPage(page, "Splitter")
    hSplitter = wx.SplitterWindow(page)

    panelOne = GridPanel(hSplitter)
    panelTwo = GridPanel(hSplitter)
    hSplitter.SplitVertically(panelOne, panelTwo)
    hSplitter.SetSashGravity(0.5)

    panelThree = RegularPanel(page)
    page.SplitHorizontally(hSplitter, panelThree)
    page.SetSashGravity(0.5)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(notebook, 1, wx.EXPAND)
    self.SetSizer(sizer)

class MainFrame(wx.Frame):
def __init__(self):
    """Constructor"""
    wx.Frame.__init__(self, None, title="Nested Splitters",
                      size=(800,600))
    panel = MainPanel(self)
    self.Show()

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

我尝试合并代码,尝试复制最后一个模式并集成我的列表框和树形图。但它似乎永远不会起作用。我只是希望能够在页面上放置一个树形图,在另一个页面上放置一些列表框。我错过了什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

让我们从创建一个简单的笔记本开始:

import random
import wx

########################################################################
class TabPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent=parent)

        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))

        btn = wx.Button(self, label="Press Me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)

########################################################################
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""        
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)
        self.tab_num = 3

        self.notebook = wx.Notebook(panel)
        tabOne = TabPanel(self.notebook)
        self.notebook.AddPage(tabOne, "Tab 1")

        tabTwo = TabPanel(self.notebook)
        self.notebook.AddPage(tabTwo, "Tab 2")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)

        btn = wx.Button(panel, label="Add Page")
        btn.Bind(wx.EVT_BUTTON, self.addPage)
        sizer.Add(btn)

        panel.SetSizer(sizer)
        self.Layout()

        self.Show()

    #----------------------------------------------------------------------
    def addPage(self, event):
        """"""
        new_tab = TabPanel(self.notebook)
        self.notebook.AddPage(new_tab, "Tab %s" % self.tab_num)
        self.tab_num += 1

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

您将注意到我们正在为笔记本中的每个标签创建一个面板实例。笔记本是标签面板的父级。然后我们调用笔记本的AddPage方法将面板实例与标签名称一起添加到笔记本中。

在您的示例中,您已在标签面板中创建了一个面板。这里的问题是,当它的父级也是一个面板时,这个嵌套面板不会自动展开。坦率地说,你根本不需要嵌套面板。因此,为了使其工作,只需删除嵌套面板,并使每个窗口小部件的父级成为PageOne中的顶级面板。以下是使用PageOne类的示例:

import wx

class PageOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        vbox_left = wx.BoxSizer(wx.VERTICAL)
        vbox_right = wx.BoxSizer(wx.VERTICAL)

        hbox = wx.BoxSizer(wx.HORIZONTAL)

        driveList=["D:/", "E:/"]

        font = wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL)

        label = wx.StaticText(self, -1, "Audio Source") # Add type/name of source
        label.SetFont(font)
        label.SetSize(label.GetBestSize())
        self.combo1 = wx.ComboBox(self, style=wx.CB_DROPDOWN, choices=driveList)
        self.listbox = wx.ListBox(self, 1, size=(380, 220))
        vbox_left.Add(label, 0, wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER_HORIZONTAL, 5)
        vbox_left.Add(self.combo1, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.LEFT | wx.RIGHT, 15)
        vbox_left.Add(self.listbox, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 15)

        label = wx.StaticText(self, -1, "Local Disk") # Add disk name
        label.SetFont(font)
        label.SetSize(label.GetBestSize())
        self.combo2 = wx.ComboBox(self, style=wx.CB_DROPDOWN, choices=driveList)
        self.listbox = wx.ListBox(self, 0, size=(380, 220))
        vbox_right.Add(label, 0, wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER_HORIZONTAL, 5)
        vbox_right.Add(self.combo2, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.LEFT | wx.RIGHT, 15)       
        vbox_right.Add(self.listbox, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 15)

        hbox.Add(vbox_left, 0, wx.EXPAND | wx.LEFT, 20)
        hbox.Add(vbox_right, 0, wx.EXPAND | wx.RIGHT, 20)
        self.SetSizer(hbox)
        self.Layout()

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

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title='Notebooks')
        panel = wx.Panel(self)
        notebook = wx.Notebook(panel)
        page_one = PageOne(notebook)
        notebook.AddPage(page_one, 'Page 1')

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(notebook, 1, wx.EXPAND)
        panel.SetSizer(main_sizer)
        self.Show()


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

希望这个例子可以帮助您了解如何完成代码。