简体   繁体   English

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

[英]wxpython, call a class function from the other class

I'd like to run the class function in Class TabTwo when Class TabOne Onclick function is activated. 我想在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()

I got the error message as below: 我收到以下错误消息:

Traceback (most recent call last): 追溯(最近一次通话):
File "C:/Users/a0266997/Documents/Casualty/tbs2.py", line 124, in OnClick3 a = TabTwo.getTab2info() 在OnClick3中,文件“ C:/Users/a0266997/Documents/Casualty/tbs2.py”第124行a = TabTwo.getTab2info()
File "C:/Users/a0266997/Documents/Casualty/tbs2.py", line 224, in getTab2info TabTwo.PayoutYears = self.tb2input.GetValue() 在getTab2info TabTwo.PayoutYears = self.tb2input.GetValue()中的文件“ C:/Users/a0266997/Documents/Casualty/tbs2.py”,第224行
NameError: global name 'self' is not defined NameError:未定义全局名称“ self”

Review the comments. 查看评论。 There they give you the solution. 他们在那里为您提供解决方案。

Here is your code fixed. 这是您的固定代码。 I have deleted your comments and I have put some new ones: 我已删除您的评论,并添加了一些新评论:

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

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM