PyQt5 QFileDialog没有在ubuntu中返回正确的路径 [英] PyQt5 QFileDialog is not returning correct paths in ubuntu

查看:71
本文介绍了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)

但是没有得到这个列表:

But instead of getting this list:

['/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中返回正确的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆