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

2024-05-23 17:56:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我收到的错误信息如下:

Traceback (most recent call last):
File "C:/Users/a0266997/Documents/Casualty/tbs2.py", line 124, in OnClick3 a = TabTwo.getTab2info()
File "C:/Users/a0266997/Documents/Casualty/tbs2.py", line 224, in getTab2info TabTwo.PayoutYears = self.tb2input.GetValue()
NameError: global name 'self' is not defined


Tags: theselfinitdefclasspointwxnb
1条回答
网友
1楼 · 发布于 2024-05-23 17:56:11

回顾评论。他们给你解决办法。在

这是你的代码修正了。我已经删除了您的评论,并添加了一些新的评论:

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

祝你好运。在

相关问题 更多 >