简体   繁体   English

python pyqt5 将文件名添加到 getSaveFileName

[英]python pyqt5 add file name to getSaveFileName

I'm trying to add a default name to QFileDialog() the images below illustrate.我正在尝试向QFileDialog()添加默认名称,如下图所示。

This is what I get (no filename)这就是我得到的(没有文件名)

我得到的

and this is what I want to achieve without having to input it manually, I want to pass the file_name threw a function and have that name show up there.这就是我想要实现的,而无需手动输入它,我想通过file_name抛出一个函数并在那里显示该名称。 在此处输入图片说明

This is the code im trying to make to work:这是我试图使其工作的代码:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import *
import sys
class mainwindowUI(QMainWindow):
    def __init__(self):
        super(mainwindowUI, self).__init__()
        self.exportFiles('test.mp3')
    def exportFiles(self,file_name):
        filename, _ = QFileDialog.getSaveFileName(self, "Save audio file", "", "Audio Files (*.mp3)")
        if filename:
            print(filename)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = mainwindowUI()
    app.exec_()

I tried to add options:我尝试添加选项:

filename, _ = QFileDialog.getSaveFileName(self, "Save audio file", "", "Audio Files (*.mp3)", options=QFileDialog.setLabelText(file_name))

But this is incorrect and i have no idea how to make it work...但这是不正确的,我不知道如何使它工作......

Anyone know how to add a file name to save file dialog?任何人都知道如何添加文件名以保存文件对话框?

The third argument indicates the initial name:第三个参数表示初始名称:

def exportFiles(self, file_name):
    default_dir ="/home/qt_user/Documents"
    default_filename = os.path.join(default_dir, file_name)
    filename, _ = QFileDialog.getSaveFileName(
        self, "Save audio file", default_filename, "Audio Files (*.mp3)"
    )
    if filename:
        print(filename)

First create a save-as action首先创建一个另存为动作

self.saveas=QAction(QtGui.QIcon('saveas.png'),'save-as')

Add the save-as action to toolbar将另存为操作添加到工具栏

toolbar=self.addToolbar('toolbar');
toolbar.addAction(self.saveas);

Sub this for your QFileDialog code将此用于您的 QFileDialog 代码

Fn, _=QFileDialog.getSaveFileName(self,'export pdf',file_name,'Pdf files(.pdf);;All files()');

when connecting the signal to the slot do this将信号连接到插槽时,请执行此操作

Self.Saveas.toggled.connect(self.exportfiles('name of default file');

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

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