New wxNotebook-Pages stay empty in wxPython
I made some sample code to try wxNotebook, but the new inserted pages remain completely empty. If I don't use a Timer or a new Thread, but insert the pages before the MainLoop, it works. I suspect I need an Update, Refresh or something like that anywhere, but I couldn't get it to work. I use Windows 7 and 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()
This appears to be a threading problem, not a notebook problem. Modifying your code as follows creates the form that you're expecting. My best guess is that this is a scope issue, but I don't know for certain.
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()
I don't know if this will help you or not, but here's a silly demo:
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()
There's definitely some threading issues. Check out these errors I get on Ubuntu when running your code:
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.
精彩评论