如何让您的数据从输入和日期保存在tkinter的相应列下

时间:2017-11-06 10:32:36

标签: python csv tkinter

我有这个代码来保存我在csv文件中输入的条目以及期间<"日期和时间">我输入了数据,但我的挑战是期间,输入的数据保存在同一列标题下。

我怎样才能使日期保存在DATE栏目下,输入的数据保存在INPUT列下,并且时间保存为" #####"只有在AM,即使我在晚上执行任务。

This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.

1 个答案:

答案 0 :(得分:0)

发表@PM 2Ring的评论。您可以将输入加入一个列表和一个函数,然后分别放置它。

import csv
import os
from tkinter import *
import time

with open("TP.csv", "w") as db:
    writer = csv.writer(db)
    writer.writerow(["DATE", "INPUT"])# Create the file with this as heading

def save_print_data():
    global e3
    data = e3.get()
    display = time.strftime('%d/%m/%y  %I:%M:%S%p', time.localtime())
    totalinput = [display, data]
    with open("TP.csv", "a") as savedb:
        w = csv.writer(savedb)
        w.writerow(totalinput)


root = Tk()
root.geometry("400x400")

profile = StringVar()
e3 = Entry(width=50, textvariable=profile)

label1 = Label(root)
b1 = Button(root, text="Print & save", command=lambda: [save_print_data()])

e3.pack()
label1.pack()
b1.pack()

root.mainloop()

这将生成一个csv文件,如下所示:

DATE                    INPUT
06/11/17  10:58:17AM    test

我不熟悉csv模块,所以我不确定调整列的大小,但我现在正在检查你。但是,此代码会删除您所拥有的#######并至少显示日期