如何在wxPython中继承另一个类中的类?

时间:2014-08-14 18:47:24

标签: python class python-2.7 combobox wxpython

我已经在很大程度上研究了如何从wxPython中的不同类中获取变量而没有太多运气。我的问题是我想在用户关闭第二个窗口后更新主窗口中的组合框。我认为最好的方法是尝试以某种方式获取主窗口组合框变量。例如:

import wx
class oranges(wx.Frame):

    #----------Main Window---------#

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Test',size=(1024,768))
        self.frame=wx.Panel(self)
        self.tickers=['Apples','a','1234']
        self.dropdown=wx.ComboBox(self.frame,choices=self.tickers,pos=(750,62),style=wx.CB_READONLY)
        self.Bind(wx.EVT_COMBOBOX, self.get_stuff,self.dropdown)
        apples=wx.Button(self.frame,label='Click here',pos=(300,300),size=(100,100))
        self.Bind(wx.EVT_BUTTON, self.plus,apples)
    def get_stuff(self,event):
        pass
    def plus(self,event):
        class orange(wx.Frame):

            #----------Second Window---------#

            def __init__(self,parent,id):
                wx.Frame.__init__(self,parent,id,'Testing',size=(500,500))
                self.frames=wx.Panel(self)
                apples=wx.Button(self.frames,label='Collect Info',pos=(300,300),size=(100,100))
                self.Bind(wx.EVT_BUTTON, self.click,apples)
                self.what=wx.TextCtrl(self.frames,-1,'12',pos=(200,48))
            def click(self,event):
                asdf=self.what.GetValue()
                self.tickers=[]
                self.tickers.append(asdf)
                self.dropdown.Clear()
                self.dropdown.AppendItems(self.tickers)
                #Need to update dropdown#
                self.Destroy()
        if __name__ =='__main__':
            apps = wx.PySimpleApp()
            windows = orange(parent=None,id=-1)
            windows.Show()
            apps.MainLoop()
if __name__ =='__main__':
    app = wx.PySimpleApp()
    window = oranges(parent=None,id=-1)
    window.Show()
    app.MainLoop()

我真的很困惑如何解决这个问题。提前致谢!我期待着答案!

2 个答案:

答案 0 :(得分:0)

import wx
class SomeUserForm(wx.Dialog):
      def __init__(self):
          wx.Dialog.__init__(self,None,-1,"Enter User Info In this Frame")
          self.txt = wx.TextCtrl(self,-1,pos=(50,50))
          self.txt2 = wx.TextCtrl(self,-1,pos=(50,100))
          self.ok = wx.Button(self,wx.ID_OK,pos=(50,125))
      def GetValue(self):
          return self.txt.GetValue() + "::"+ self.txt2.GetValue()

class oranges(wx.Frame):
    #----------Main Window---------#
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Test',size=(1024,768))
        self.frame=wx.Panel(self)
        self.tickers=['Apples','a','1234']
        self.dropdown=wx.ComboBox(self.frame,choices=self.tickers,pos=(750,62),style=wx.CB_READONLY)
        self.Bind(wx.EVT_COMBOBOX, self.get_stuff,self.dropdown)
        apples=wx.Button(self.frame,label='Click here',pos=(300,300),size=(100,100))
        self.Bind(wx.EVT_BUTTON, self.plus,apples)
    def get_stuff(self,event):
        pass
    def plus(self,evt):
        dlg = SomeUserForm()
        if dlg.ShowModal() != wx.ID_OK:
            wx.MessageBox("User Cancelled Add!","Cancelled!")
            return
        self.dropdown.Append(dlg.GetValue())

if __name__ =='__main__':
    app = wx.PySimpleApp()
    window = oranges(parent=None,id=-1)
    window.Show()
    app.MainLoop()

可以做你想要的......

答案 1 :(得分:0)

在您的父框架中,添加一个名为UpdateComboBox(self, newVal)的方法,该方法接收新字符串...然后,因为您的弹出窗口是其中的子项,就在您在孩子中调用self.Destroy之前, self.GetParent().UpdateComboBox(asdf)(其中asdf来自您的示例,即您要传回的字符串)