Search code examples
pythontkintertext-filestkinter-entry

Writing an Entry Input to a text file


from tkinter import *

root = Tk()
writeList = []
myFile = open("details.txt","w")

nameLabel = Label(root,text = "Input your name: ")
nameLabel.grid(column = 1,row = 0)

nameEntry = Entry(root)
nameEntry.grid(column = 2,row = 0)

name = nameEntry.get()
writeList.extend(("Name:",name))

ageLabel = Label(root,text = "Enter your age:")
ageLabel.grid(column = 1,row = 1)

ageEntry = Entry(root)
ageEntry.grid(column = 2,row = 1)

age = ageEntry.get()
writeList.extend(("Age:",age))

def writeFile(writeList):
    for x in range(len(writeList)):
        write = writeList[x]
        myFile.write(write)
        myFile.write("\n")

    myFile.close()

writeButton = Button(root,text = "Write to a file",command = writeFile(writeList),bg = "turquoise")
writeButton.grid(column = 1,row = 3)

root.mainloop()

so basically it wont write what I input in the entry to a text file where have I gone wrong ...... I had to try using a button so it would write after the input should I write the entries in the function instead of in the for loop


Solution

  • There are a few things that need to be fixed here first.

    1. You have the get() and extend() in the global namespace and not in a function. What this will cause is the 2 methods being called at __init__ and basically you will always have and empty sting. You want to use a function to call these things so that you get the data when you need it.

    2. Your button command is also calling your function at __init__ because you are not actually saving a reference to the function. To fix this you need to get rid of the parentheses or use a lambda.

    3. I would use import tkinter as tk over from tkinter import *. This will prevent any accidental overwriting of methods.

    4. I would use with open instead of open() and close(). The with statement will auto close the file once the function has completed.

    Below I have cleaned up your code a bit and added the needed changes. Let me know if you have any questions.

    Example:

    import tkinter as tk
    
    root = tk.Tk()
    write_list = []
    
    tk.Label(root, text="Input your name: ").grid(column=1, row=0)
    tk.Label(root, text="Enter your age:").grid(column=1, row=1)
    name_entry = tk.Entry(root)
    age_entry = tk.Entry(root)
    name_entry.grid(column=2, row=0)
    age_entry.grid(column=2, row=1)
    
    
    def write_file():
        name = name_entry.get()
        age = age_entry.get()
        write_list.extend(("Name:", name))
        write_list.extend(("Age:", age))
        with open("details.txt", "w") as my_file:
            for x in range(len(write_list)):
                write = write_list[x]
                my_file.write(write)
                my_file.write("\n")
    
    
    tk.Button(root, text="Write to a file", command=write_file, bg="turquoise").grid(column=1, row=3)
    root.mainloop()
    

    Results:

    enter image description here

    File contains the following text:

    Name:
    Mike - SMT
    Age:
    32