2011-04-07 20 views
2

我正在使用wx創建一個用於運行其他程序的GUI。我有兩個Notebook選項卡:第一個用於輸入,第二個用於輸出我的程序。我想設置它,以便用戶將在InputPanel中指定一個用於輸出程序的目錄,並且OutputPanel中的ComboBox將使用此目錄的內容進行更新。我的代碼一個精簡版的樣子:基於另一個筆記本標籤中的TextCtrl()更改wx.ComboBox()中的選擇

#!/usr/bin/python 

    import glob 
    import wx 

    class InputPanel(wx.Panel): 
     dirF = 0 
     def __init__(self, parent): 
      wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) 

      self.dirFileText = wx.StaticText(self, label="Output Directory:") 
      self.dirFile = wx.TextCtrl(self, value="fuda", style = wx.TE_PROCESS_ENTER) 
      self.Bind(wx.EVT_TEXT, self.EvtText, self.dirFile) 

      sizer = wx.BoxSizer(wx.HORIZONTAL) 
      sizer.Add(self.dirFileText, 0, wx.ALL, 2) 
      sizer.Add(self.dirFile, 0, wx.ALL|wx.EXPAND,2) 
      self.SetSizer(sizer) 
      sizer.Fit(self) 

     def EvtText(self, event): 
      event.GetString() 
      InputPanel.dirF = self.dirFile.GetValue() 

    class OutputPanel(wx.Panel): 
     def __init__(self, parent): 
      wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) 

      self.dirText = wx.StaticText(self, label="Choice?") 
      self.dirT = wx.ComboBox(self, choices='', style=wx.CB_READONLY) 
      self.dirT.SetItems(glob.glob("%s/*.dat" % (InputPanel.dirF))) 
      self.Bind(wx.EVT_COMBOBOX, self.onSelect, self.dirT) 

      sizer = wx.BoxSizer(wx.HORIZONTAL) 
      sizer.Add(self.dirText, 0, wx.ALL, 2) 
      sizer.Add(self.dirT, 0, wx.ALL|wx.EXPAND, 2) 
      self.SetSizer(sizer) 
      sizer.Fit(self) 

     def onSelect(self, event): 
      event.GetSelection() 

    class Notebook(wx.Notebook): 
     def __init__(self, parent): 
      wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT) 

      tabOne = InputPanel(self) 
      self.AddPage(tabOne, "Input") 
      tabTwo = OutputPanel(self) 
      self.AddPage(tabTwo, "Output") 

      self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged) 
      self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING, self.onPageChanging) 

     def onPageChanged(self, event): 
      event.Skip() 

     def onPageChanging(self, event): 
      event.Skip() 

class MainWindow(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "nmrPipeFit", size=(600,400)) 
     panel = wx.Panel(self) 

     # Setting up the menu 
     filemenu = wx.Menu() 
     menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Exit the program") 
     menubar = wx.MenuBar() # Create the menubar 
     menubar.Append(filemenu, "&File") # Add file menu to menubar 
     self.SetMenuBar(menubar) # Add menubar to frame 
     # Set menu events 
     self.Bind(wx.EVT_MENU, self.OnExit, menuExit) 

     notebook = Notebook(panel) 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5) 
     panel.SetSizer(sizer) 
     sizer.Fit(self) 
     self.Layout() 
     self.Show() 

    def OnExit(self, event): 
     self.Close(True) # Close the frame 

if __name__== "__main__": 
    app=wx.PySimpleApp() 
    frame = MainWindow() 
    app.MainLoop() 

我嘗試了好幾種不同的事情,才能爲組合框與從輸入面板目錄中的變化來更新選項列表中,但沒有運氣。任何幫助將不勝感激。

感謝,提前, 邁克爾·萊瑟姆

回答

2

嗯,有一個醜陋的黑客攻擊方式做到這一點和優雅的方式來做到這一點。其實,可能有幾個黑客。讓我們看看最常見的一個:

在筆記本的頁面更改事件中,可以告訴輸出面板執行如下操作:inputPanelRef.dirFile.GetValue(),然後根據需要更新組合框的條目。您可能需要從Notebook小部件中執行此操作。

我喜歡在面板之間傳遞信息的方式是使用Pubsub。這裏有一個很好的例子: http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

+0

那麼我的方式是醜陋的嗎? :-D – Jake 2011-04-07 20:17:39

+0

這是一個很好的方式來做到這一點。不一定是壞的,但如果你碰巧改變了類,部件或方法名稱的名稱,很容易中斷。我知道像parent.widget.getter這樣的東西在wxPython郵件列表中通常是不鼓勵的。 YMMV當然。 – 2011-04-07 21:00:28

+0

其實現在我看着它,你的方法看起來很酷,不會做我想到的。 – 2011-04-07 21:02:23

0

您可以添加一個getter和setter每個面板的像這樣:

#!/usr/bin/python 

import wx 
import os 

class InputPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) 

     self.dirFileText = wx.StaticText(self, label="Output Directory:") 
     self.dirFile = wx.TextCtrl(self, value=".", style = wx.TE_PROCESS_ENTER) 
     self.Bind(wx.EVT_TEXT, self.EvtText, self.dirFile) 

     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     sizer.Add(self.dirFileText, 0, wx.ALL, 2) 
     sizer.Add(self.dirFile, 0, wx.ALL|wx.EXPAND,2) 
     self.SetSizer(sizer) 
     sizer.Fit(self) 

     self._dirF = '.' 

    def EvtText(self, event): 
     event.GetString() 
     self._dirF = self.dirFile.GetValue() 

    def GetDirF(self): 
     return self._dirF 

class OutputPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) 

     self.dirText = wx.StaticText(self, label="Choice?") 
     self.dirT = wx.ComboBox(self, choices='', style=wx.CB_READONLY) 
     self.Bind(wx.EVT_COMBOBOX, self.onSelect, self.dirT) 

     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     sizer.Add(self.dirText, 0, wx.ALL, 2) 
     sizer.Add(self.dirT, 0, wx.ALL|wx.EXPAND, 2) 
     self.SetSizer(sizer) 
     sizer.Fit(self) 

    def onSelect(self, event): 
     event.GetSelection() 

    def setDirT(self, dirT): 
     self.dirT.Clear() 
     for f in dirT: 
      self.dirT.Insert(f, 0) 


class Notebook(wx.Notebook): 
    def __init__(self, parent): 
     wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT) 

     self.tabOne = InputPanel(self) 
     self.AddPage(self.tabOne, "Input") 
     self.tabTwo = OutputPanel(self) 
     self.AddPage(self.tabTwo, "Output") 

     self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged) 

    def onPageChanged(self, event): 
     if event.GetSelection() == 1: 
      dirf = self.tabOne.GetDirF() 
      files = os.listdir(dirf) 
      self.tabTwo.setDirT(files) 
     event.Skip() 

class MainWindow(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "nmrPipeFit", size=(600,400)) 
     panel = wx.Panel(self) 

     # Setting up the menu 
     filemenu = wx.Menu() 
     menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Exit the program") 
     menubar = wx.MenuBar() # Create the menubar 
     menubar.Append(filemenu, "&File") # Add file menu to menubar 
     self.SetMenuBar(menubar) # Add menubar to frame 
     # Set menu events 
     self.Bind(wx.EVT_MENU, self.OnExit, menuExit) 

     notebook = Notebook(panel) 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5) 
     panel.SetSizer(sizer) 
     sizer.Fit(self) 
     self.Layout() 
     self.Show() 

    def OnExit(self, event): 
     self.Close(True) # Close the frame 

if __name__== "__main__": 
    app=wx.PySimpleApp() 
    frame = MainWindow() 
    app.MainLoop() 

您可能要清理它了一點,但是這說明了你怎麼可能做到這一點。

相關問題