新的wxNotebook-Pages在wxPython中保持空白

时间:2011-01-22 12:31:03

标签: wxpython wxnotebook

我制作了一些示例代码来尝试wxNotebook,但新插入的页面仍然是完全空的。 如果我不使用Timer或新线程,但在MainLoop之前插入页面,则可以正常工作。 我怀疑我需要更新,刷新或类似的东西,但我无法让它工作。 我使用Windows 7和Python 2.7。

import wx
import threading

class addNewPages(threading.Thread):  
    def __init__(self, nb):
        super(addNewPages, self).__init__()
        self.nb = nb
    def run(self):
        self.p = 1
        for i in range(1, 5, 1):
            threading.Timer(i, self.adp).start()
    def adp(self):
        self.newpage = Page(self.nb)
        self.newpage.SetBackgroundColour(wx.GREEN)
        wx.StaticText(self.newpage, -1, "PAGE "+str(self.p), style=wx.ALIGN_CENTRE)
        wx.CallAfter(self.nb.AddPage, self.newpage, "Page "+str(self.p))
        self.p += 1

class Page(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        wx.StaticText(self, -1, "This is a Page object", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Notebook Test")
        p = wx.Panel(self)
        self.nb = wx.Notebook(p)
        self.page1 = Page(self.nb)

        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    mf = MainFrame()
    mf.Show()
    mf.nb.AddPage(mf.page1, "Testseite 1")
    th = addNewPages(mf.nb)
    th.start()
    app.MainLoop()

3 个答案:

答案 0 :(得分:2)

这似乎是一个线程问题,而不是笔记本问题。按如下方式修改代码会创建您期望的表单。我最好的猜测是这是一个范围问题,但我不确定。

import wx

def run(nb):
    for i in range(1, 5, 1):
        adp(nb, i)

def adp(nb, p):
    newpage = Page(nb)
    newpage.SetBackgroundColour(wx.GREEN)
    wx.StaticText(newpage, -1, "PAGE "+str(p), style=wx.ALIGN_CENTRE)
    wx.CallAfter(nb.AddPage, newpage, "Page "+str(p))

class Page(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        wx.StaticText(self, -1, "This is a Page object", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Notebook Test")
        p = wx.Panel(self)
        self.nb = wx.Notebook(p)
        self.page1 = Page(self.nb)

        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    mf = MainFrame()
    mf.Show()
    mf.nb.AddPage(mf.page1, "Testseite 1")
    run(mf.nb)
    app.MainLoop()

答案 1 :(得分: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,
                          "self.notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)
        self.tabNum = 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")
        btn = wx.Button(panel, label="Add Page")
        btn.Bind(wx.EVT_BUTTON, self.onButton)

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

        panel.SetSizer(sizer)
        self.Layout()

        self.Show()

    #----------------------------------------------------------------------
    def onButton(self, event):
        """"""
        tab = TabPanel(self.notebook)
        self.notebook.AddPage(tab, "Tab %s" % self.tabNum)
        self.tabNum += 1

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

答案 2 :(得分:1)

肯定存在一些线程问题。查看运行代码时我在Ubuntu上遇到的这些错误:

jake@swift:~/Dropbox/dev/wxfun$ python otherfile.py
The program 'python' received an X Window System error.
This probably reflects a bug in the program.
The error was 'RenderBadPicture (invalid Picture parameter)'.
  (Details: serial 1066 error_code 161 request_code 149 minor_code 6)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)
jake@swift:~/Dropbox/dev/wxfun$ python otherfile.py
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.