简体   繁体   English

PyQt5 QFileDialog 在 ubuntu 中没有返回正确的路径

[英]PyQt5 QFileDialog is not returning correct paths in ubuntu

I'm using this bit of code to open a file dialog and return the selected file names (PyQt5, Ubuntu)我正在使用这段代码打开文件对话框并返回选定的文件名(PyQt5,Ubuntu)

QtWidgets.QFileDialog.getOpenFileNames(self, 'Open files', self.__target, self.__open_f)

But instead of getting this list:但不是得到这个列表:

['/home/python/Downloads/addresses.csv', '/home/python/Downloads/airtravel.csv']

I am getting this list:我得到这个列表:

['/run/user/1000/doc/9f194012/addresses.csv', '/run/user/1000/doc/885466d0/airtravel.csv']

here is my code:这是我的代码:

import os
import sys
from mods import fixqt
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon

from mods.csvdata import DataCSV
from mods.err_report import report_error
from mods.save_xl import save_excel_file
from ui.mainwindow import Ui_mwWCS

# this is the value of self.__target
home = os.path.expanduser("~/Desktop")
icon_path = os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "ui"), "Icon.ico")
open_filter = "CSV files (*.csv)"
save_filter = "Excel Workbook (*.xlsx)"
input_data = DataCSV([])

class MainWindow(QtWidgets.QMainWindow):  # window = qtw.QMainWindow()
    def __init__(self, title="", mw_home="", op_filter="All files (*.*)", sv_filter="All files (*.*)", parent=None):
        super().__init__(parent)
        self.__title = title 
        self.ui = Ui_mwWCS() 
        self.ui.setupUi(self)  
        self.__target = mw_home
        self.__open_f = op_filter
        self.__save_f = sv_filter
        self.__excel_file = ""
        self.setWindowIcon(QIcon(icon_path))
        self.__input_data = DataCSV([])

    def __show_dialog(self):
        return QtWidgets.QFileDialog.getOpenFileNames(self, 'Open files', self.__target, self.__open_f)

    def __set_csv(self, lst):
        self.__input_data.set_files_list(lst)
        # print(lst)
        self.__input_data.open_csv_files()
        self.__input_data.exception_entries()
        self.__input_data.set_boxes_number()
        self.__input_data.set_plates_number()

    def on_add_clicked(self):
        try:
            list_names, _ = self.__show_dialog()

            self.ui.lstInput.addItems(list_names)
            self.__set_csv(list_names)

        except Exception as e:
            report_error("Error occurred (ADD)", e)

Can you please help on how can I get the proper filenames?你能帮助我如何获得正确的文件名吗?

Update: trying my code in a terminal worked fine, could it be a problem related to pyCharm?更新:在终端中尝试我的代码工作正常,这可能是与 pyCharm 相关的问题吗?

来自python终端的屏幕截图

@musicamante, thank you for your help. @musicamante,感谢您的帮助。 The answer lays with DontUseNativeDialog if I'm running my code with PyCharm.如果我使用 PyCharm 运行我的代码,答案在于DontUseNativeDialog Running it outside PyCharm, that flag isn't required.在 PyCharm 之外运行它,不需要该标志。

Same issue happening for me aswell, finally i figured out the curprit is pycharm (in my case), try run your code in terminal and you will see it works fine.同样的问题也发生在我身上,最后我发现curprit是pycharm(在我的情况下),尝试在终端中运行你的代码,你会看到它工作正常。 and after snap package your application it will also run smooth.在 snap 打包您的应用程序后,它也将运行顺畅。 So this was not a problem for me now.所以现在这对我来说不是问题。

I am using PyCharm and I had the same problem.我正在使用 PyCharm 并且遇到了同样的问题。 The option "DontUseNativeDialog" as suggested by @musicamante also solved the problem for me. @musicamante 建议的“DontUseNativeDialog”选项也为我解决了这个问题。

QtWidgets.QFileDialog.getOpenFileName(parent=self, options=QtWidgets.QFileDialog.DontUseNativeDialog)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM