简体   繁体   English

使用Tkinter将用户输入导出到.csv文件

[英]Using Tkinter to export user input to a .csv file

I have a tkinter code setup that will give the user seven entry prompts, and a "Submit" button. 我有一个tkinter代码设置,它将为用户提供七个输入提示和一个“提交”按钮。 I want to be able to export whatever the user types into the entry box, to a .csv file. 我希望能够将输入框中的任何用户类型导出到.csv文件。 I have used the Python tkinter docs, and many other resources, including this website, but cannot find an answer. 我使用过Python tkinter文档和许多其他资源,包括这个网站,但找不到答案。 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. 我已经研究过如何使用“get()”函数,因为这似乎是一个常见的答案,但我对tkinter的了解有限。 Any and all help is tremendously appreciated. 任何和所有的帮助都非常感谢。

You are on the right track using get(), which will return the contents of a tkinter Entry widget. 您使用get()在正确的轨道上,它将返回tkinter Entry小部件的内容。

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. 我使用了一个条目来说明如何从Entry小部件完成retreiving文本并将内容写入.csv文件。

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. 通过创建Entry并将其分配给self.e,您可以通过在App类的writeToFile函数中调用self.e.get()来访问文本值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM