2011-01-22 3 views
0

サンプルコードを使ってwxNotebookを試しましたが、挿入された新しいページは完全に空のままです。 私はタイマーまたは新しいスレッドを使用せず、MainLoopの前にページを挿入すると機能します。 私はどこでも更新、更新などが必​​要だと思っていますが、動作させることができませんでした。 私はWindows 7とPython 2.7を使用しています。新しいwxNotebook-wxPythonでページが空になる

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() 

答えて

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() 
+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, 
          "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() 
1

スレッドに関する問題があります。コード実行時にUbuntuで発生するこれらのエラーを確認してください:

[email protected]:~/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.) 
[email protected]:~/Dropbox/dev/wxfun$ python otherfile.py 
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.