wxpython,从另一个类调用一个类函数

时间:2018-02-19 19:40:45

标签: python function class tabs wxpython

当Class TabOne Onclick功能被激活时,我想在Class TabTwo中运行类函数。

import wx

class TabOne(wx.Panel):
    def __init__(self, parent):
        self.button = wx.Button(self, 12, "Submit", wx.Point(300, 590))   
        wx.EVT_BUTTON(self, 12, self.OnClick)

    def OnClick(self, event):
        a = TabTwo.getTab2info()
        print a

class TabTwo(wx.Panel):
    def __init__(self, parent):
        self.tb2name= wx.StaticText(self,-1, "The number of pay out years:", wx.Point(20, 190))
        self.tb2input=wx.TextCtrl(self, 31, "5",wx.Point(315, 190), wx.Size(150, -1))

    @classmethod
    def getTab2info(cls):
        TabTwo.PayoutYears = self.tb2input.GetValue()
        return(TabTwo.PayoutYears)

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Information", size=(1200, 700))

    # Create a panel and notebook (tabs holder)
        p = wx.Panel(self)
        nb = wx.Notebook(p)

    # Create the tab windows
        tab1 = TabOne(nb)
        tab2 = TabTwo(nb)

    # Add the windows to tabs and name them.
        nb.AddPage(tab1, "Input")
        nb.AddPage(tab2, "Model Paramters")

    # Set noteboook in a sizer to create the layout
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

我收到如下错误消息:

  

追踪(最近的呼叫最后):
    文件" C:/Users/a0266997/Documents/Casualty/tbs2.py" ;,第124行,在OnClick3中       a = TabTwo.getTab2info()
    文件" C:/Users/a0266997/Documents/Casualty/tbs2.py",第224行,在getTab2info中       TabTwo.PayoutYears = self.tb2input.GetValue()
  NameError:全局名称' self'未定义

1 个答案:

答案 0 :(得分:0)

查看评论。他们在那里为您提供解决方案。

这是您的代码固定的。我已删除您的评论,并添加了一些新评论:

import wx

class TabOne(wx.Panel):
    # We pass the main frame as the last parameter of the constructor
    def __init__(self, parent, mainFrame):
        # Call to superclass:
        wx.Panel.__init__(self, parent=parent)
        # Let's assign the main frame to a member variable of this class
        self._mainFrame = mainFrame
        self.button = wx.Button(self, 12, "Submit", wx.Point(300, 590))   
        wx.EVT_BUTTON(self, 12, self.OnClick)

    def OnClick(self, event):  
        # I use the main frame to access the instance of class TabTwo.      
        a = self._mainFrame.tab2.getTab2info()                
        print a

class TabTwo(wx.Panel):
    # We pass the main frame as the last parameter of the constructor
    def __init__(self, parent, mainFrame):
        # Call to superclass:
        wx.Panel.__init__(self, parent=parent)
        # Let's assign the main frame to a member variable of this class
        self._mainFrame = mainFrame

        self.tb2name= wx.StaticText(self,-1, "The number of pay out years:", wx.Point(20, 190))
        self.tb2input=wx.TextCtrl(self, 31, "5",wx.Point(315, 190), wx.Size(150, -1))

    def getTab2info(self):
        #payoutYears is local variable only used in this method.        
        payoutYears = self.tb2input.GetValue()
        return(payoutYears)

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Information", size=(1200, 700))
        p = wx.Panel(self)
        nb = wx.Notebook(p)
        self.tab1 = TabOne(nb, self)
        self.tab2 = TabTwo(nb, self)        
        nb.AddPage(self.tab1, "Input")
        nb.AddPage(self.tab2, "Model Paramters")
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

祝你好运。