2012-08-23 12 views
1

我搜查了很多做這個,但沒有什麼我試過的工作。現在,作爲最後一次嘗試,我正在嘗試使用pubsub,但我沒有任何價值,這就是爲什麼我現在要求幫助:)。這是一個最小的(盡我所能做到:) :)我想要的例子。 PanelB獲取列表(框)中的信息,並且當選擇了某些項目時,PanelA應根據他進行更改。 預先感謝您。如何在函數中重建wxpython中的面板?

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

這很好,你正在使用pubsub與'MainFrame'對話。它保持'PanelB'封裝 – acattle

回答

0

這是不漂亮的解決方案,基本上你可以摧毀紅砂糖然後再調用面板,也許是這樣的:

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

希望這可以幫助。

更新(2012年8月24日):添加一些代碼

歐凱我們不應該破壞面板。我正在使用你的代碼並修改了一下。我刪除了全局變量name並將changeName(name)函數添加到PanelA,以便當調用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() 
+0

現在我想它可以做這項工作,我會盡力做到這一點:)。這不起作用(我之前在我的程序中嘗試過,現在只是這個最簡單的例子),它破壞了面板,但我無法重建它。 – AsVHEn

+0

@AsVHEn我已更新我的答案,爲您的代碼添加一些修改 – Kyomuu

+0

不錯,工作。非常感謝你(現在我會嘗試如果我能在我的程序中做到這一點:)) – AsVHEn