Python 使用函数中声明的变量,并在不同类的另一个函数中使用它们
首先,我已经大大缩短了我的代码,因为它有1000多行代码。 我想做的是获取Python 使用函数中声明的变量,并在不同类的另一个函数中使用它们,python,wxpython,Python,Wxpython,首先,我已经大大缩短了我的代码,因为它有1000多行代码。 我想做的是获取getchain中声明的变量,并在def createfigure中使用它们。他们来自不同的班级。 目前,我得到的错误是getchain()正好接受2个参数(给定1个) 有人能告诉我哪里出了问题,以及如何获取我在def getchain(self,event)中使用的变量,并在def createfigure中使用这些变量。当一个方法在python中被定义为使用“self”作为参数时,它所应用的对象将作为参数隐式传递。然后
getchain
中声明的变量,并在def createfigure
中使用它们。他们来自不同的班级。
目前,我得到的错误是getchain()正好接受2个参数(给定1个)
有人能告诉我哪里出了问题,以及如何获取我在
def getchain(self,event)
中使用的变量,并在def createfigure
中使用这些变量。当一个方法在python中被定义为使用“self”作为参数时,它所应用的对象将作为参数隐式传递。然后,必须在括号中显式提供您声明的任何其他参数。在您提供的摘录的倒数第二行,您没有提供所需的参数“event” 你不应该那样做。如果您真的非常想,那么最好通过使用“self”预挂起变量来创建变量类属性。因此,如果要访问另一个类中的“flag”变量,请将其设置为“self.flag”
然后您可以使用OtherF.flag访问它。然而,我认为如果您需要从另一个类中获取一些内容,我建议使用pubsub来传递它。以下是一个教程:
事件
传递给您的OtherF.getchain()
调用。您是否在询问如何访问该事件
?
#!/usr/bin/python
import MainPanel
import wx
########################################################################
class OtherFrame(wx.Frame):##open PDB frame
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY, "Secondary Frame")
panel = wx.Panel(self)##create panel
chain = wx.Button(panel, label = "OK", pos=(100,80),size=(53,25))##button create
self.Bind(wx.EVT_BUTTON,self.getchain,chain)##bind event to button go to getchain method
def getchain(self,event):
global flag
flag = 1
import OtherFrame
#######
class MainPanel(wx.Panel):##main frame
""""""
#----------------------------------------------------------------------
def __init__(self, parent, size = (5000,5000)):
"""Constructor"""
wx.Panel.__init__(self, parent=parent, size = (5000,5000))
self.frame = parent
Publisher().subscribe(self.showFrame, ("show.mainframe"))
def showFrame(self, msg):
#------------------------------------------------------------------
def createfigure():
OtherF = OtherFrame.OtherFrame()
OtherF.getchain()
print flag