【发布时间】: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