在笔记本wxPython中使用listctrl

时间:2013-11-04 14:30:43

标签: wxpython

我有一个问题..笔记本中的wxPython listctrl

我创建了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()

3 个答案:

答案 0 :(得分:0)

我使用GetParent()获取MyFrame而不是SecondFrame

def AddList(self, evt):
    # PageOne -> Notebook -> Panel -> MyFrame ( 3x GetParent() )
    self.GetParent().GetParent().GetParent().SecondFrame.list1.InsertStringItem(0,'Hello')

MyFrame我必须向所有self.添加SecondFrame(不一定是MainFrame

也许可以通过事件绑定来完成,但我不知道。

完整代码:

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):
        # PageOne -> Notebook -> Panel -> MyFrame ( 3x GetParent() )
        self.GetParent().GetParent().GetParent().SecondFrame.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)

        # add self.
        self.MainFrame = PageOne(nb)
        self.SecondFrame = PageTwo(nb)

        # add self.
        nb.AddPage(self.MainFrame, "One")
        nb.AddPage(self.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()

答案 1 :(得分: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()

或者你可以使用pubsub,它更干净,更不容易破坏。这是一种方法:

    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之前版本),那么您可能需要使用pubsub的旧API,您可以在此处阅读:

另请注意,我删除了按钮的ID。您不应将按钮的id设置为较低的数字,因为它可能由wxPython在内部使用。如果你必须创建一个id,建议你这样做:

btnId = wx.NewId()

答案 2 :(得分:0)

另一种方法是使用父节点在子节点之间进行调解,并在面板上创建接口方法,以缩短调用时间并防止内部更改。

import wx


class PageOne(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.query_find_btn = wx.Button(self, label="BTN", size=(40, 40))

    def bind_find_btn(self, handler):
        self.query_find_btn.Bind(wx.EVT_BUTTON, handler)


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

    def insert_list_item(self, text):
        self.list1.InsertStringItem(0, text)


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

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

        self.mainframe = PageOne(nb)
        self.secondframe = PageTwo(nb)

        nb.AddPage(self.mainframe, "One")
        nb.AddPage(self.secondframe, "Two")

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

        self.mainframe.bind_find_btn(self.on_button)

    def on_button(self, event):
        self.secondframe.insert_list_item('Hello')


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

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