首页 > 解决方案 > 我想将“文件名”作为参数传递给其他函数调用,但总是返回“无”作为值

问题描述

我正在使用 PyQT4 开发 GUI:代码如下所示 -

def fileopening(): 
    filename = QtGui.QFileDialog.getOpenFileName(mainwindow, 'Select file') 
    print(filename) 
    return filename


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mainwindow = QtGui.QWidget()
    ui = Ui_mainwindow()
    ui.setupUi(mainwindow)
    filename = ui.browser.clicked.connect(fileopening)
    print(filename)
    ui.progressBar.setValue(100)
    ui.progesslabel.setText('Kindly fill in the fields and proceed for splitting.')
    ui.splitbutton.clicked.connect(pressed)
    ui.resetparameter.clicked.connect(reset)

    mainwindow.show()
    app.exec_()

当提示 UI 时,它会将打印输出显示为“无”,并且从函数的定义中它会在函数调用中获取文件名,但它没有返回任何值。有人可以提出可能的原因吗?我想将“文件名”作为参数传递给其他函数调用,但它总是返回“无”作为值。

标签: pythonuser-interfacepyqt4

解决方案


你有没有尝试过

return QtGui.QFileDialog.getOpenFileName(mainwindow, 'Select file')

您可以将名称文件名设为全局。

def fileopening(): 
    global filename
    filename = QtGui.QFileDialog.getOpenFileName(mainwindow, 'Select file') 

推荐阅读