簡體   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