本文介绍了PyQt5 QFileDialog 没有在 ubuntu 中返回正确的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这段代码打开一个文件对话框并返回选定的文件名(PyQt5、Ubuntu)

I'm using this bit of code to open a file dialog and return the selected file names (PyQt5, Ubuntu)

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

但不是得到这个列表:

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

我得到了这份清单:

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

这是我的代码:

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?

更新:在终端中尝试我的代码效果很好,这可能是与 pyCharm 相关的问题吗?

Update:trying my code in a terminal worked fine, could it be a problem related to pyCharm?

推荐答案

@musicamante,谢谢你的帮助.如果我使用 PyCharm 运行我的代码,答案就在于 DontUseNativeDialog.在 PyCharm 之外运行它,不需要该标志.

@musicamante, thank you for your help. The answer lays with DontUseNativeDialog if I'm running my code with PyCharm. Running it outside PyCharm, that flag isn't required.

这篇关于PyQt5 QFileDialog 没有在 ubuntu 中返回正确的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 03:13