繁体   English   中英

QT:如何将程序生成的文件保存到用户机器中

[英]QT: how to save a generated file by the program into user's machine

我有一个 PyQt 程序,它从用户那里获取一个文件进行处理,然后相应地创建一个新文件。 我希望用户能够将新创建的文件下载/保存到他们的机器/桌面...

这是上传代码:

def open_dialog_box(self):
    #To just print the name of file
     filename = QFileDialog.getOpenFileName()
     filename = filename[0]
     basename = os.path.basename(filename)

def pushButton_2_handler(self):
    path = QFileDialog.getOpenFileName()
    path = path[0]
    basename = os.path.basename(path)
    filename, file_extension = os.path.splitext(basename)
    print(path)
    print(filename)
    print(file_extension)
    self.textBrowser_fileSelectedName.clear()
    self.textBrowser_fileSelectedName.append(str(basename))

您想使用另一个QFileDialog ,但这个 QFileDialog 将设置为返回用户键入的选定文件夹/自定义名称。

def save_file(self):

    # Open a dialog so that the user can select a target location.
    dialog = QFileDialog()
    dialog.setWindowTitle("Select a location to save your file.")
    dialog.setAcceptMode(QFileDialog.AcceptSave)
    dialog.exec_()
    if not dialog.selectedFiles(): # If the user closed the choice window without selecting anything.
        return
    else:
        path_to_file = dialog.selectedFiles()[0]

    # Create that file and work on it, for example:
    with open(path_to_file + ".txt") as file:
        file.write("Hello there!")

暂无
暂无

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

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