1

我搜索了很多来做到这一点,但我尝试过的却一无所获。现在,作为最后一次尝试,我正在尝试使用 pubsub,但我一无所获,这就是我现在寻求帮助的原因:)。这是我想要的一个最小的例子(尽我所能:))。PanelB gets information in a list (box), and when someone of the items is selected, PanelA should change according to him. 先感谢您。

from wx.lib.pubsub import Publisher
import wx

global name
name  = 'none, please select an item'

class PanelA(wx.Panel):
   def __init__(self, parent, name):
       wx.Panel.__init__(self, parent)
       self.hbox = wx.BoxSizer(wx.HORIZONTAL)
       self.vbox = wx.BoxSizer(wx.VERTICAL)
       str = name
       txt = wx.StaticText(self, -1, "You have selected " + str, (20, 100))
       self.hbox.Add(txt, 1, wx.EXPAND | wx.ALL, 20)

class PanelB(wx.Panel):
   def __init__(self, parent):
       wx.Panel.__init__(self, parent)

       self.hbox = wx.BoxSizer(wx.HORIZONTAL)
       self.vbox = wx.BoxSizer(wx.VERTICAL)

       self.listbox = wx.ListBox(self, -1)
       self.hbox.Add(self.listbox, 1, wx.EXPAND | wx.ALL, 20)

       self.btnPanel = wx.Panel(self, -1)      
       self.new = wx.Button(self.btnPanel,label='Add', size=(90, 30))
       self.new.Bind(wx.EVT_BUTTON, self.NewItem)

       self.vbox.Add((-1, 20))
       self.vbox.Add(self.new)

       self.btnPanel.SetSizer(self.vbox)
       self.hbox.Add(self.btnPanel, 0.6, wx.EXPAND | wx.RIGHT, 20)
       self.SetSizer(self.hbox)

       self.Bind(wx.EVT_LISTBOX, self.onSelect)

    def onSelect(self, event):
       name_selected = self.listbox.GetStringSelection()
       Publisher().sendMessage(("ListBox"), name_selected)

def NewItem(self, event):
       text = wx.GetTextFromUser('Nombre', 'Programa a salvar')
       if text != '':
           self.listbox.Append(text)

class MainFrame(wx.Frame):
   def __init__(self, parent, id, title, *args, **kw):

       wx.Frame.__init__(self, parent, id, title, size = (800,300))

       self.splitter = wx.SplitterWindow(self, -1, style=wx.SP_3D)

       self.lc1 = PanelB(self.splitter)
       Publisher().subscribe(self.OnSelect, ("ListBox"))
       self.lc2 = PanelA(self.splitter, name)
       self.splitter.SplitVertically(self.lc1, self.lc2)

       sizer = wx.BoxSizer(wx.HORIZONTAL)
       sizer.Add(self.splitter, 1, wx.EXPAND)
       self.SetSizer(sizer)

       self.Centre()
       self.Show(True)

   def OnSelect(self, name_selected):
       name = name_selected
       #I stucked here

if __name__ == "__main__":
   app = wx.App()
   frame = MainFrame(None,-1,'Mi aplicacion')
   app.MainLoop()
4

1 回答 1

0

这不是很好的解决方案,基本上你可以破坏PanelA然后再次调用该面板,可能是这样的:

def OnSelect(self, name_selected):
    self.lc2.Destroy()
    self.lc2 = PanelA(self.splitter, name_selected.data)

希望这能有所帮助。

更新(08/24/2012):添加了一些代码

好吧,我们不应该破坏面板。我正在使用您的代码并对其进行了一些修改。我删除了全局变量并向PanelAname添加了changeName(name)函数,以便在调用时它会调用. 它并不漂亮,但希望这能有所帮助。MainFrame.onSelect()changeName()

import wx
from wx.lib.pubsub import Publisher

class PanelA(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        name = "none, please select an item"
        self.txt = wx.StaticText(self, -1, "You have selected " + name,
                                 (50, 100))
        self.hbox.Add(self.txt, 1, wx.EXPAND|wx.ALL, 30)

    def changeName(self, name):
        self.hbox.Hide(self.txt)
        self.hbox.Remove(self.txt)
        self.txt = wx.StaticText(self, -1, "You have selected " + name, (50, 100))
        self.hbox.Add(self.txt, 1, wx.EXPAND|wx.ALL, 30)


class PanelB(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.vbox = wx.BoxSizer(wx.VERTICAL)

        self.listbox = wx.ListBox(self, -1)
        self.hbox.Add(self.listbox, 1, wx.EXPAND|wx.ALL, 20)

        self.btnPanel = wx.Panel(self, -1)      
        self.new = wx.Button(self.btnPanel,label='Add', size=(90, 30))
        self.new.Bind(wx.EVT_BUTTON, self.NewItem)

        self.vbox.Add((-1, 20))
        self.vbox.Add(self.new)

        self.btnPanel.SetSizer(self.vbox)
        self.hbox.Add(self.btnPanel, 0.6, wx.EXPAND|wx.RIGHT, 20)
        self.SetSizer(self.hbox)

        self.Bind(wx.EVT_LISTBOX, self.onSelect)

    def onSelect(self, event):
        name_selected = self.listbox.GetStringSelection()
        Publisher().sendMessage("ListBox", name_selected)

    def NewItem(self, event):
        text = wx.GetTextFromUser('Nombre', 'Programa a salvar')
        if text != '':
            self.listbox.Append(text)


class MainFrame(wx.Frame):

   def __init__(self, parent, id, title, *args, **kw):

       wx.Frame.__init__(self, parent, id, title, size = (800,300))
       self.splitter = wx.SplitterWindow(self, -1, style=wx.SP_3D)
       Publisher().subscribe(self.OnSelect, "ListBox")

       self.lc1 = PanelB(self.splitter)
       self.lc2 = PanelA(self.splitter)
       self.splitter.SplitVertically(self.lc1, self.lc2)

       self.sizer = wx.BoxSizer(wx.HORIZONTAL)
       self.sizer.Add(self.splitter, 1, wx.EXPAND)
       self.SetSizer(self.sizer)
       self.Layout()

       self.Centre()

   def OnSelect(self, name_selected):
       name = name_selected
       self.lc2.changeName(name.data)


if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MainFrame(None,-1,'Mi aplicacion')
    frame.Show()
    app.MainLoop()
于 2012-08-24T01:17:48.843 回答