python代码, def save_answer_file(self): file_path = filedialog.asksaveasfilename() if file_path: with open(file_path, "w") as file: file.write(json.dumps(self.answers)) 我需要设置默认保存扩展名为txt
时间: 2024-03-17 10:41:30 浏览: 104
filedialog.zip_NEW_dialog_file_open_opendialog
你可以在 `file_path` 中指定默认的保存扩展名为txt,修改后的代码如下:
```python
def save_answer_file(self):
file_path = filedialog.asksaveasfilename(defaultextension=".txt")
if file_path:
with open(file_path, "w") as file:
file.write(json.dumps(self.answers))
```
这样,当用户没有指定保存文件的扩展名时,默认保存为txt格式。
阅读全文