Using Tkinter to export user input to a .csv file

11,749

You are on the right track using get(), which will return the contents of a tkinter Entry widget.

I have used a single Entry to illustrate how you can accomplish retreiving text from Entry widgets and writing the contents to a .csv file.

from tkinter import * 
import csv

class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.output()

    def output(self):
        Label(text='Name:').pack(side=LEFT,padx=5,pady=5)
        self.e = Entry(root, width=10)
        self.e.pack(side=LEFT,padx=5,pady=5)

        self.b = Button(root, text='Submit', command=self.writeToFile)
        self.b.pack(side=RIGHT,padx=5,pady=5)

    def writeToFile(self):
        with open('WorkOrderLog.csv', 'a') as f:
            w=csv.writer(f, quoting=csv.QUOTE_ALL)
            w.writerow([self.e.get()])

if __name__ == "__main__":
    root=Tk()
    root.title('Auto Logger')
    root.geometry('1000x100')
    app=App(master=root)
    app.mainloop()
    root.mainloop()

By creating the Entry and assigning it to self.e, you are able to access the text value by calling self.e.get() in the App class' writeToFile function.

Share:
11,749
saurusrexx
Author by

saurusrexx

Updated on June 04, 2022

Comments

  • saurusrexx
    saurusrexx almost 2 years

    I have a tkinter code setup that will give the user seven entry prompts, and a "Submit" button. I want to be able to export whatever the user types into the entry box, to a .csv file. I have used the Python tkinter docs, and many other resources, including this website, but cannot find an answer. This is the code i have so far:

    import Tkinter 
    from Tkinter import *
    from ttk import *
    import csv
    
    class App(Frame):
        def tile():
            Label(text='Enter Information Below').pack(side=TOP,padx=15,pady=15)
    
    def output(self):
        with open('WorkOrderLog.csv', 'a') as f:
            w=csv.writer(f, quoting=csv.QUOTE_ALL)
    
            Label(text='Name:').pack(side=LEFT,padx=5,pady=5)
            Entry(root, width=10).pack(side=LEFT,padx=5,pady=5)
    
            Label(text='1:').pack(side=LEFT,padx=5,pady=5)
            Entry(root, width=10).pack(side=LEFT,padx=5,pady=5)
    
            Label(text='2:').pack(side=LEFT,padx=5,pady=5)
            Entry(root, width=10).pack(side=LEFT,padx=5,pady=5)
    
            Label(text='3:').pack(side=LEFT,padx=5,pady=5)
            Entry(root, width=10).pack(side=LEFT,padx=5,pady=5)
    
            Label(text='4:').pack(side=LEFT,padx=5,pady=5)
            Entry(root, width=10).pack(side=LEFT,padx=5,pady=5)
    
            Label(text='5:').pack(side=LEFT,padx=5,pady=5)
            Entry(root, width=10).pack(side=LEFT,padx=5,pady=5)
    
            Label(text='6:').pack(side=LEFT,padx=5,pady=5)
            Entry(root, width=10).pack(side=LEFT,padx=5,pady=5)
    
            Button(root, text='Submit', command=w.writerow([Entry,Entry,Entry,Entry,Entry,Entry,Entry])).pack(side=RIGHT,padx=5,pady=5)
    
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.output()
    
    root=Tk()
    root.title('Auto Logger')
    root.geometry('1000x100')
    app=App(master=root)
    app.mainloop()
    root.mainloop()
    

    Im lost as tho where to go from here. I have researched how to to use the "get()" function, as that seems to be a common answer, but my knowledge of tkinter is limited. Any and all help is tremendously appreciated.