【问题标题】:Passing data from one fucntion to another. Python将数据从一个函数传递到另一个函数。 Python
【发布时间】:2018-07-29 10:44:27
【问题描述】:

我正在开发一个应用程序,并且我已经使用 qt5 创建者制作了一个 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 comput/Application/Sheet.xlsx) 并将其插入process() 函数(也由单击触发),而不会再次触发对话窗口。如果我只是在process() 中调用selectFile() 函数,就会发生这种情况。

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

【问题讨论】:

  • 你试过def process(self, file_name): ...并从方法selectFile调用它而不是打印只是做process(fileName)
  • 您是同时保存多个文件名还是只存储一个?
  • 为什么不将文件名传递给定义def process(self, filename)process函数
  • @Yohboy 只存一个
  • 那么为什么不使用 fileName 作为类属性呢?您可以使用 self.fileName 来存储文件名,因此不需要在函数中传递它。

标签: python pyqt event-handling pyqt5 desktop-application


【解决方案1】:

您应该使用 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 是否存在。

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    您需要做的就是在单击按钮时获取打开的文件路径。并调用file_path上的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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多