【问题标题】:wxpython, call a class function from the other classwxpython,从另一个类调用一个类函数
【发布时间】:2018-07-30 02:20:03
【问题描述】:

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

我收到如下错误信息:

回溯(最近一次通话最后一次):
OnClick3 中的文件“C:/Users/a0266997/Documents/Casualty/tbs2.py”,第 124 行 a = TabTwo.getTab2info()
文件“C:/Users/a0266997/Documents/Casualty/tbs2.py”,第 224 行,在 getTab2info TabTwo.PayoutYears = self.tb2input.GetValue()
NameError:未定义全局名称“self”

【问题讨论】:

  • 类方法中没有self。你没有对象可以参考。毕竟它是一个class 方法,而不是一个instance 方法。类方法不能引用任何对象的变量。当您调用它时,您也许可以将实例(self 变量)传递给类方法。但是为什么getTab2info 无论如何都需要是一个类方法呢?有什么理由不能成为正常的方法吗?
  • 我是新手。如果我将函数更改为即时方法: def getTab2info(): TabTwo.PayoutYears = self.tb2input.GetValue() return(TabTwo.PayoutYears) 我收到错误:TypeError: unbound method getTab2info() must be called with TabTwo instance as first争论(一无所获)。
  • 如果我将调用类 TabTwo 函数的行更改为: a = TabTwo().getTab2info() 我收到如下错误 TypeError: __init__() 恰好需要 2 个参数(1 个给定)。我会很感激任何帮助
  • 我想也许你最好解释一下你希望在这里实现什么。调用该方法不是目的,而是达到目的的手段。这里的实际目标是什么。目前,您有两个单独的笔记本选项卡,出于某种原因,它们需要彼此了解很多。我无法理解推理。
  • 我想要实现的是:当我点击 TabOne 中的“提交”按钮时,python 会收集用户在 TabOne 和 TabTwo 上的输入。我设置了2个标签是因为在一个标签中设置所有用户输入,界面会太大

标签: python function class tabs wxpython


【解决方案1】:

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

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

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

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多