2013-11-04 41 views
0

我有一個問題.. wxPython的listctrl的筆記本如何在筆記本電腦使用的listctrl的wxPython

我創建2標籤使用的筆記本電腦。

我在第一個選項卡中添加了按鈕,並在第二個選項卡中添加了Listctrl。

如果我點擊按鈕,將Listctrl中的值添加到第二個選項卡。 如何解決這個問題?

import wx 

class PageOne(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.query_find_btn = wx.Button(self, 4, "BTN", (40,40)) 
     self.Bind(wx.EVT_BUTTON, self.AddList, id = 4) 

    def AddList(self, evt): 
     self.list1.InsertStringItem(0,'Hello') 

class PageTwo(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.list1 = wx.ListCtrl(self,-1,wx.Point(0,0),wx.Size(400,400),style=wx.LC_REPORT | wx.SUNKEN_BORDER) 
    self.list1.InsertColumn(0,'values') 

class MyFrame(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self,parent,id,title,size=(400,400),pos=wx.Point(100,100), 
     style=wx.SYSTEM_MENU |wx.CAPTION) 

     p = wx.Panel(self) 
     nb = wx.Notebook(p) 

     MainFrame = PageOne(nb) 
     SecondFrame = PageTwo(nb) 

     nb.AddPage(MainFrame, "One") 
     nb.AddPage(SecondFrame, "Two") 

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


class MyApp(wx.App): 
    def OnInit(self): 
     self.frame=MyFrame(None,-1,'Unknown.py') 
     self.frame.Centre() 
     self.frame.Show() 
     return True 

if __name__ == '__main__': 
    app = MyApp(False) 
    app.MainLoop() 
+0

[筆記本wxPython中使用的listctrl]的可能重複(http://stackoverflow.com/questions/19769937/using- listctrl-in-notebook-wxpython) – furas

+0

不要用相同的問題創建新的線程!我把這個問題發送給管理員作爲重複! – furas

回答

0

有幾種方法可以解決這個問題。你可以這樣做的愚蠢的方式,像這樣:

import wx 

class PageOne(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.query_find_btn = wx.Button(self, -1, "BTN", (40,40)) 
     self.Bind(wx.EVT_BUTTON, self.AddList, self.query_find_btn) 

    def AddList(self, evt): 
     frame = self.GetTopLevelParent() 
     frame.pageTwo.list1.InsertStringItem(0,'Hello') 

class PageTwo(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.list1 = wx.ListCtrl(self,-1,wx.Point(0,0),wx.Size(400,400),style=wx.LC_REPORT | wx.SUNKEN_BORDER) 
     self.list1.InsertColumn(0,'values') 

class MyFrame(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self,parent,id,title,size=(400,400),pos=wx.Point(100,100), 
     style=wx.SYSTEM_MENU |wx.CAPTION) 

     p = wx.Panel(self) 
     nb = wx.Notebook(p) 

     self.pageOne = PageOne(nb) 
     self.pageTwo = PageTwo(nb) 

     nb.AddPage(self.pageOne, "One") 
     nb.AddPage(self.pageTwo, "Two") 

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


class MyApp(wx.App): 
    def OnInit(self): 
     self.frame=MyFrame(None,-1,'Unknown.py') 
     self.frame.Centre() 
     self.frame.Show() 
     return True 

if __name__ == '__main__': 
    app = MyApp(False) 
    app.MainLoop() 

或者您可以使用發佈訂閱,這是更清潔,更容易破裂。這裏有一種方法:

import wx 
from wx.lib.pubsub import pub 

class PageOne(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.query_find_btn = wx.Button(self, -1, "BTN", (40,40)) 
     self.Bind(wx.EVT_BUTTON, self.AddList, self.query_find_btn) 

    def AddList(self, evt): 
     pub.sendMessage("listctrlListener", message="Hello") 

class PageTwo(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.list1 = wx.ListCtrl(self,-1,wx.Point(0,0),wx.Size(400,400),style=wx.LC_REPORT | wx.SUNKEN_BORDER) 
     self.list1.InsertColumn(0,'values') 
     pub.subscribe(self.updateListCtrl, "listctrlListener") 

    #---------------------------------------------------------------------- 
    def updateListCtrl(self, message): 
     """""" 
     self.list1.InsertStringItem(0, message) 

class MyFrame(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self,parent,id,title,size=(400,400),pos=wx.Point(100,100), 
     style=wx.SYSTEM_MENU |wx.CAPTION) 

     p = wx.Panel(self) 
     nb = wx.Notebook(p) 

     MainFrame = PageOne(nb) 
     SecondFrame = PageTwo(nb) 

     nb.AddPage(MainFrame, "One") 
     nb.AddPage(SecondFrame, "Two") 

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


class MyApp(wx.App): 
    def OnInit(self): 
     self.frame=MyFrame(None,-1,'Unknown.py') 
     self.frame.Centre() 
     self.frame.Show() 
     return True 

if __name__ == '__main__': 
    app = MyApp(False) 
    app.MainLoop() 

這是使用pubsub的新API。你可以在這裏看到使用它的另一個例子:

  • ​​

如果你碰巧使用舊版本的wxPython的(預2.8.10),那麼你可能需要使用發佈訂閱的舊的API,你可以讀到這裏:

另外請注意,I R emoved按鈕的ID。您不應該將按鈕的ID設置爲較低的數字,因爲它可能會在wxPython內部使用。建議如果你要創建一個id,你應該這樣做是這樣的:

btnId = wx.NewId() 
相關問題