1

我有一个问题..笔记本中的 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()
4

3 回答 3

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()
于 2013-11-04T15:28:32.240 回答
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()
于 2013-11-04T16:19:27.910 回答
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()
于 2013-11-05T18:36:28.590 回答