【发布时间】:2019-04-06 11:56:51
【问题描述】:
我需要有一个输入询问文件路径,当用户用文件名填充文件路径时,程序必须用这个文件名保存一个文件。在输入下,我需要一个文本和多个输入只是一个答案。我该怎么做?
【问题讨论】:
-
那你的尝试是什么?
标签: python python-2.7 tkinter
我需要有一个输入询问文件路径,当用户用文件名填充文件路径时,程序必须用这个文件名保存一个文件。在输入下,我需要一个文本和多个输入只是一个答案。我该怎么做?
【问题讨论】:
标签: python python-2.7 tkinter
使用filedialog 中的tkinter,
完整代码演示:
from tkinter import Tk, Label, Button, Text, filedialog
class MyFirstGUI:
def __init__(self, master):
self.master = master
master.title("A simple GUI")
self.text = Text(master)
self.text.pack()
self.save_button = Button(master, text="Save as...", command=self.open)
self.save_button.pack()
def open(self):
self._filetypes = [
('Text', '*.txt'),
('All files', '*'),
]
self.filename = filedialog.asksaveasfilename(defaultextension='.txt',
filetypes = self._filetypes)
f = open(self.filename, 'w')
f.write(self.text.get('1.0', 'end'))
f.close()
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
所以只需要做保存文件的功能就行了,用open保存(写)
【讨论】:
filedialog 是一个字符串,另外,您能否检查一下您的代码中是否还有其他filedialogs?如果有,请重命名变量.