0

I have a variable which contains a string that I want to write in a .txt file using python. But I don't know how to do so. e1 is the variable which contains the string.

from tkinter import * 

root = Tk()
root.title("records manager")
root.geometry("600x200")

label0 = Label(root, text="")
label0.pack()
label1 = Label(root, text="Welcome to records manager (づ。◕‿‿◕。)づ", font=("ariel bold", 20))
label1.pack()

leftframe = Frame(root, padx="20", pady="20")
leftframe.pack(side=LEFT)
rightframe = Frame(root, padx="20", pady="20")
rightframe.pack(side=RIGHT)

def add():
    patients = open("Patients.txt", "a")
    e1 = Entry1.get()
    patients.write(e1 + "\n")
    patients.close()

New = Label(leftframe, text="New Patients", font=("ariel bold", 12))
New.grid(row=0)
Entry1 = Entry(leftframe, width=40, border=5)
Entry1.grid(row=0, column=1)
button1 = Button(rightframe, width=20, command=add(), text="Add")
button1.grid(row=0, column=0)

root.mainloop()
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • 1
    That would be `e1` not `en1`? What goes wrong? I think you want to change `command=add()` to `command=add`. In the first case you called the function "add" and tried to make its return value the command for Button. – tdelaney Oct 27 '20 at 17:16
  • The duplicate I found for you is too complicated for your problem, sorry. The underlying issue is the same: `command=add()` means that `add` *runs immediately*, and then the *result of that* is used as the button command. You are supposed to just use *the name of the function itself*: `command=add`. This is not a special Tkinter thing; it's a built-in Python idea that many people just don't encounter until they try to use it in Tkinter. – Karl Knechtel Oct 27 '20 at 17:18

0 Answers0