![](/img/trans.png)
[英]In Pyramid, how can I read from a file on the end user's machine (no need to save it)?
[英]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.