将数据从一个功能传递到另一个功能。python

将数据从一个功能传递到另一个功能。python,python,pyqt,event-handling,pyqt5,desktop-application,Python,Pyqt,Event Handling,Pyqt5,Desktop Application,我正在开发一个应用程序,我用qt5 creator制作了一个UI。我有一个绑定到按钮的函数 self.dataChooseBtn.clicked.connect(self.selectFile) self.data_processing.clicked.connect(self.process) def selectFile(self): options = QFileDialog.Options() options |= QFileDialog.DontUseNati

我正在开发一个应用程序,我用qt5 creator制作了一个UI。我有一个绑定到按钮的函数

self.dataChooseBtn.clicked.connect(self.selectFile)
self.data_processing.clicked.connect(self.process)

def selectFile(self):    
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    fileName, _ = QFileDialog.getOpenFileName(self,"Выберите стандартизированную выборку", "","All Files (*);;Python Files (*.py)", options=options)
    if fileName:
        print(fileName)
        return fileName
当按下这个按钮时,我会看到一个对话框窗口,在那里我可以选择一个文件

另外,我还有一个函数,可以处理所选的文件。现在,文件的路径和名称是硬编码的

def process(self):
    file_location = "/Users/Graygood/Desktop/Science\ comput/Application/Sheet.xlsx"
    sample = pd.read_excel('Sheet.xlsx', sheetname ='Sheet1', index_col=None, na_values=['NA'])
我想要的是获得
selectFile()
函数的输出(由单击触发) (例如:/Users/Graygood/Desktop/Science compute/Application/Sheet.xlsx) 并将其插入
process()
函数(也由单击触发),而不会再次触发对话框窗口。如果我只调用
process()
one中的
selectFile()
函数,就会发生这种情况

def process(self):
    fileName = self.selectFile()
    file_location = fileName
    sample = pd.read_excel('Sheet.xlsx', sheetname ='Sheet1', index_col=None, na_values=['NA'])

您应该使用fileName作为类属性,因此存储您的fileName,以便在不将其传递到所有函数的情况下重用它时能够跟踪它

只需将
selectFile
更改为:

def selectFile(self):    
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    fileName, _ = QFileDialog.getOpenFileName(self,"Выберите стандартизированную выборку", "","All Files (*);;Python Files (*.py)", options=options)
    if fileName:
        print(fileName)
        self.fileName = fileName
然后在
process(self)
中调用
self.fileName

为了避免错误,您还应该在init方法中声明它:
self.fileName=None
,并在尝试使用它之前始终测试self.fileName是否存在


希望有此帮助。

您应该将文件名用作类属性,以便存储文件名,以便在不将其传递到所有函数的情况下重复使用它时能够跟踪

只需将
selectFile
更改为:

def selectFile(self):    
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    fileName, _ = QFileDialog.getOpenFileName(self,"Выберите стандартизированную выборку", "","All Files (*);;Python Files (*.py)", options=options)
    if fileName:
        print(fileName)
        self.fileName = fileName
然后在
process(self)
中调用
self.fileName

为了避免错误,您还应该在init方法中声明它:
self.fileName=None
,并在尝试使用它之前始终测试self.fileName是否存在


希望有此帮助。

您只需单击按钮获取打开的文件路径即可。并调用文件路径上的process方法

def selectFile(self):    
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    # read the file path
    file_path, _ = QFileDialog.getOpenFileName(self,"Выберите стандартизированную выборку", "","All Files (*);;Python Files (*.py)", options=options)
    if file_path:
        # print the file_path and call process on it.
        print(file_path)
        self.process(file_path)
        return file_path

def process(self, file_path):
    # read the file at path and process it
    sample = pd.read_excel(file_path, sheetname ='Sheet1', index_col=None, na_values=['NA'])
    print("processed")

您所需要做的就是在单击按钮时获取打开的文件路径。并调用文件路径上的process方法

def selectFile(self):    
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    # read the file path
    file_path, _ = QFileDialog.getOpenFileName(self,"Выберите стандартизированную выборку", "","All Files (*);;Python Files (*.py)", options=options)
    if file_path:
        # print the file_path and call process on it.
        print(file_path)
        self.process(file_path)
        return file_path

def process(self, file_path):
    # read the file at path and process it
    sample = pd.read_excel(file_path, sheetname ='Sheet1', index_col=None, na_values=['NA'])
    print("processed")

您是否尝试过
def进程(self,file_name):…
并通过方法调用它
selectFile
而不是打印just do
process(fileName)
您是同时有多个文件名,还是只存储一个文件名?为什么不将文件名传递给
进程
函数定义
def进程(self,fileName)
@Yohboy只有一个存储空间,那么为什么不使用fileName作为类属性呢?您可以使用self.fileName来存储文件名,因此不需要在函数中传递它。您是否尝试过
def process(self,file\u name):…
并通过方法
selectFile
调用它,而不是直接打印
process(fileName)
您是同时有多个文件名,还是只有一个文件名要存储?为什么不将文件名传递给
进程
函数定义
def进程(self,fileName)
@Yohboy只有一个文件名要存储?为什么不将文件名用作类属性?您可以使用self.fileName来存储文件名,因此不需要在函数中传递它。