【发布时间】:2018-07-02 10:46:23
【问题描述】:
我想将输入框的输入保存到.txt 文件中,它适用于第一个代码而不是第二个代码,但我需要使用的是第二个代码。
代码 1:
import tkinter as tk
def f():
def save():
a = t.get()
f = open((a + '.txt'), 'w')
f.write(a)
f.close()
return
top = tk.Tk()
t = tk.StringVar()
e = tk.Entry(top, textvariable = t).pack()
b = tk.Button(top, text = 'Save as a file', command = save).pack()
top.mainloop()
f()
代码 2:
import tkinter as tk
root = tk.Tk()
def f():
def save():
a = t.get()
f = open((a + '.txt'), 'w')
f.write(a)
f.close()
return
top = tk.Tk()
t = tk.StringVar()
e = tk.Entry(top, textvariable = t).pack()
b = tk.Button(top, text = 'Save as a file', command = save).pack()
top.mainloop()
button = tk.Button(root, text="Button",command=f).pack()
root.mainloop()
【问题讨论】:
-
将 "top = tk.Tk()" 更改为 "top = tk.Toplevel()" 有效(这基本上是 Lafexlos 链接的摘要)。
标签: python tkinter file-handling