-2

All, I have attempted this a few different ways and am still struggling here.

I want to have someone write in this Entry Box and then once submit is hit, it should write the text to the .txt file. I'm obviously not very good.

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 Answers1

1

You need to provide the command, not the result of the command, to the Button. IOW leave off the (). It should be like this: benter = Button(window, text="Submit", command = save). Also you need to convert now to a string before writing, like this: f.write(str(now))

Novel
  • 13,406
  • 2
  • 25
  • 41