Python 3.x 从另一个类检索变量
我有两个类,如何将pathentry变量传递给下一个类?实际上,pathentry变量是在文本条目中输入的文件的路径名Python 3.x 从另一个类检索变量,python-3.x,arguments,parameter-passing,Python 3.x,Arguments,Parameter Passing,我有两个类,如何将pathentry变量传递给下一个类?实际上,pathentry变量是在文本条目中输入的文件的路径名 class LabelBrowseButtonWidget(AbsLabelInputWidget): """Class created to create button widgets in the header of the gui""" def create_input(self): filepath_var = StringVa
class LabelBrowseButtonWidget(AbsLabelInputWidget):
"""Class created to create button widgets in the header of the gui"""
def create_input(self):
filepath_var = StringVar()
self.Button = ttk.Button(self,
text="Select Input File",
command=lambda: filepath_var.set(self.getFilePath())
)
self.Button.pack(fil=tk.X, padx=0, expand=True)
pathentry = ttk.Entry(self, textvariable=filepath_var)
pathentry.pack(fill=tk.X, padx=5, expand=False)
return pathentry
def getFilePath(self):
filepath = filedialog.askopenfilename(filetypes=(("Excel Files", "*.xls"),
("Excel Files", "*.xlsx")))
return filepath
class LabelButtonWidget(AbsLabelInputWidget):
"""Button created for executing the import of the input file"""
def create_input(self, *args):
self.Button = ttk.Button(self,
text="Import Input File",
command=lambda: self.fileImport()
)
self.Button.pack(fil=tk.X, padx=5, expand=True)
def fileImport(self):
pathentry = LabelBrowseButtonWidget.create_input(self.pathentry.get())
print(pathentry)
# print("now what?")
我想我在这里有点迷路了。我不太明白 是否希望从任何地方都可以访问变量?如果是,请使用
global
。定义变量时,首先放在以下行:
全局变量
然后在你访问它之前
创建类实例时是否要将变量传递给类?如果是,请使用\uuuuu init\uuuu
函数:
class classname(object):
def __init__(self, variable):
self.variable=variable
然后,在创建实例时:
instance=classname(variable)
以及从类中访问变量:
self.variable
从外部:
instance.variable
还是只想检索另一类属性的变量?如果是:
instance.variable
我不太明白 是否希望从任何地方都可以访问变量?如果是,请使用
global
。定义变量时,首先放在以下行:
全局变量
然后在你访问它之前
创建类实例时是否要将变量传递给类?如果是,请使用\uuuuu init\uuuu
函数:
class classname(object):
def __init__(self, variable):
self.variable=variable
然后,在创建实例时:
instance=classname(variable)
以及从类中访问变量:
self.variable
从外部:
instance.variable
还是只想检索另一类属性的变量?如果是:
instance.variable
问题在哪里?在
def fileImport
或LabelButtonWidget
中,我需要将LabelbrowseButtonwidget类中路径输入小部件中的文本传递给Label Button小部件中的fileImport。这容易吗?我怎样才能知道问题在哪里才能回答你的问题?对不起,还是初学者。哪里有问题?在def fileImport
或LabelButtonWidget
中,我需要将LabelbrowseButtonwidget类中路径输入小部件中的文本传递给Label Button小部件中的fileImport。这容易吗?我怎样才能知道问题在哪里才能回答你的问题?对不起,还是初学者。