2011-12-22 23 views
0

如何在一個框架中的一個框架中引用另一個框架中另一個框架中的輸入變量?wxpython - 從另一個框架返回輸入變量

我基本上想在我的wxpython gui的菜單欄中創建一個'選項'選項卡,單擊它時會打開一個允許用戶更改某些變量的新框架。但是,當我嘗試稍後引用這些變量時,我得到 AttributeError:類型對象'OptionsPanel'沒有屬性'Input1'

我有兩個面板和兩個框架定義爲類。 這裏是我的完整代碼:

import wx 
class MainFrame(wx.Frame): 
    def __init__(self,title): 
     wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,300)) 
     menuBar = wx.MenuBar() 
     menu = wx.Menu() 
     m_options = menu.Append(wx.ID_EDIT, "&Options", "Options") 
     self.Bind(wx.EVT_MENU, self.OnOptions, m_options) 
     menuBar.Append(menu, "&Options") 
     self.SetMenuBar(menuBar) 
     panel=MainPanel(self) 
    def OnOptions(self, event): 
     frame = OptionsFrame("Options Frame") 
     frame.Show() 
class OptionsFrame(wx.Frame): 
    def __init__(self,title): 
     wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,200))  
     panel=OptionsPanel(self) 
class OptionsPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.label = wx.StaticText(self, label="Input Value", pos=(40,60)) 
     self.Input1 = wx.TextCtrl(self, value="1.0", pos=(80,80), size=(60,-1)) 
class MainPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.button =wx.Button(self, label="GO", pos=(60,100)) 
     self.Bind(wx.EVT_BUTTON, self.OnClick,self.button) 
    def OnClick(self,event): 
     MyVariable= OptionsPanel.Input1.GetValue() #This won't work! 
     print dt0 
if __name__=="__main__": 
    app = wx.App(redirect=False) 
    frame = MainFrame("Multiple Frames Attempt") 
    frame.Show() 
    app.MainLoop() 

提前感謝!

回答

1

一個問題是,類MainPanel中的OnClick方法引用類OptionsPanel,而不是引用類OptionsFrame中的類OptionsPanel('panel')的實例化。

import wx 
class MainFrame(wx.Frame): 
    def __init__(self,title): 
     wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,300)) 
     menuBar = wx.MenuBar() 
     menu = wx.Menu() 
     m_options = menu.Append(wx.ID_EDIT, "&Options", "Options") 
     self.Bind(wx.EVT_MENU, self.OnOptions, m_options) 
     menuBar.Append(menu, "&Options") 
     self.SetMenuBar(menuBar) 
     panel=MainPanel(self) 
     self.options_frame = None 
    def OnOptions(self, event): 
     self.options_frame = OptionsFrame("Options Frame") 
     self.options_frame.Show() 
    def GetInput1Value(self): 
     if self.options_frame is not None: 
      return(self.options_frame.options_panel.Input1.GetValue()) 
     else: 
      return('None') 
class OptionsFrame(wx.Frame): 
    def __init__(self,title): 
     wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(200,200))  
     self.options_panel=OptionsPanel(self) 
class OptionsPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.label = wx.StaticText(self, label="Input Value", pos=(40,60)) 
     self.Input1 = wx.TextCtrl(self, value="1.0", pos=(80,80), size=(60,-1)) 
class MainPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.parent_frame = parent 
     self.button =wx.Button(self, label="GO", pos=(60,100)) 
     self.Bind(wx.EVT_BUTTON, self.OnClick,self.button) 
    def OnClick(self,event): 
     MyVariable= self.parent_frame.GetInput1Value() 
     print MyVariable 
if __name__=="__main__": 
    app = wx.App(redirect=False) 
    frame = MainFrame("Multiple Frames Attempt") 
    frame.Show() 
    app.MainLoop() 
+0

感謝大衛,你有建議我需要在代碼中進行哪些修改來解決這個問題嗎? – Mike 2011-12-22 21:12:49

+0

我承認這不是pythonic,但看到我編輯的答案。 – 2011-12-22 21:28:52

+0

對代碼做出這些更改是否有意義?你看到在類MainPanel中的OnClick現在可以訪問Input1 TextCtrl? – 2011-12-22 23:33:51

相關問題