簡體   English   中英

PyQt5 QFileDialog 在 ubuntu 中沒有返回正確的路徑

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

我正在使用這段代碼打開文件對話框並返回選定的文件名(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)

你能幫助我如何獲得正確的文件名嗎?

更新:在終端中嘗試我的代碼工作正常,這可能是與 pyCharm 相關的問題嗎?

來自python終端的屏幕截圖

@musicamante,感謝您的幫助。 如果我使用 PyCharm 運行我的代碼,答案在於DontUseNativeDialog 在 PyCharm 之外運行它,不需要該標志。

同樣的問題也發生在我身上,最后我發現curprit是pycharm(在我的情況下),嘗試在終端中運行你的代碼,你會看到它工作正常。 在 snap 打包您的應用程序后,它也將運行順暢。 所以現在這對我來說不是問題。

我正在使用 PyCharm 並且遇到了同樣的問題。 @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