从EntryBox中获取文本,然后将其写入txt文件

时间:2019-03-10 04:27:55

标签: python tkinter

全部, 我尝试了几种不同的方法,但仍在这里苦苦挣扎。

我想让某人在此输入框中书写内容,然后单击“提交”后,应将文本写入.txt文件。我显然不是很好。

import datetime
from tkinter import *

def save():
    with open("text.txt", "a") as f:
        now = datetime.datetime.now()
        test = TxtComplaint.get()
        test = str(test)
        f.write(test)
        f.write(now)

window = Tk()
window.title("Documentation Window")
lbl = Label(window, text = "Enter In The Employee's Information")
TxtComplaint = Text(window, height = '10', width = '30')
benter = Button(window, text="Submit", command = save())
TxtComplaint.pack()

ee = Entry(window)
eelbl = Label(window, text = "Whats the name of the employee?")
eename = str(lbl)
lbl.pack()
benter.pack()
ee.pack()
eelbl.pack()

window.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要向Button提供命令,而不是命令结果。 IOW离开()。应该是这样的:benter = Button(window, text="Submit", command = save)。另外,您还需要在写入之前立即将其转换为字符串,例如:f.write(str(now))