Python 如何读取另一个类中的属性?

Python 如何读取另一个类中的属性?,python,attributes,pyside,Python,Attributes,Pyside,仍然是Python的初学者,所以要友善:) 仅供参考:Python 2.7.5、PySide 1.1.2、OSX 10.8 简单的问题。我有这个功能: def openFileDialog(self): import os path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open File", os.getcwd()) self.label.setText(path) print(path) 我想在另一个不属

仍然是Python的初学者,所以要友善:) 仅供参考:Python 2.7.5、PySide 1.1.2、OSX 10.8 简单的问题。我有这个功能:

def openFileDialog(self):
    import os
    path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open File", os.getcwd())
    self.label.setText(path)
    print(path)
我想在另一个不属于同一类的函数中使用这个“路径”。。。我尝试了不同的方法在我的其他函数中显示此路径,例如,我尝试了

print(testWindow.openFileDialog.path)
但它不起作用。你会怎么做。。。? 谢谢大家!

您想要创建路径

def openFileDialog(self):
    import os
    path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open File", os.getcwd())
    self.label.setText(path)
    return path
这样你可以这样称呼它

path = testWindow.openFileDialog()
并将其存储在局部变量中。那就随你的便吧

print path
如果
testWindow
是包含
openFileDialog
的类,则必须创建第一个的实例,然后调用该方法

x = testWindow()
path = x.openFileDialog()
你想找到那条路

def openFileDialog(self):
    import os
    path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open File", os.getcwd())
    self.label.setText(path)
    return path
这样你可以这样称呼它

path = testWindow.openFileDialog()
并将其存储在局部变量中。那就随你的便吧

print path
如果
testWindow
是包含
openFileDialog
的类,则必须创建第一个的实例,然后调用该方法

x = testWindow()
path = x.openFileDialog()

将您的
def openFileDialog(self):
更改为
返回路径
,然后您可以捕获并使用它

path = testWindow.openFileDialog()

将您的
def openFileDialog(self):
更改为
返回路径
,然后您可以捕获并使用它

path = testWindow.openFileDialog()

我添加了返回路径。我的另一个函数是def essai():import pyfits path=testWindow.openFileDialog()print(path),但我得到“TypeError:unbound method openFileDialog()必须以testWindow实例作为第一个参数调用(改为什么也没有得到)”。。。我遗漏了什么?编辑,假设
testWindow
是包含
openFileDialog
的类,我添加了返回路径。我的另一个函数是def essai():import pyfits path=testWindow.openFileDialog()print(path),但我得到“TypeError:unbound method openFileDialog()必须以testWindow实例作为第一个参数调用(改为什么也没有得到)”。。。我错过了什么?编辑,假设
testWindow
是包含
openFileDialog