私はcsvファイルのエントリに入力したものを期間< "dateとtime">と一緒に保存するこのコードを持っています私の挑戦は期間であり、入力されたデータは同じ列見出しの下に保存されます。エントリと日付のデータをtkinterの各列の下に保存する方法
DATE列に日付が保存され、入力されたデータがINPUT列に保存され、時刻が "#####"として保存されます。夜。
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 current_date_time(): # the time saves only in am aven when i perform the task at night
display = time.strftime('%d/%m/%y %I:%M:%S', time.localtime())
with open ("TP.csv", "a") as csvfile:
w = csv.writer(csvfile)
w.writerow([display])
def save_print_data():
global e3
data = e3.get()
with open ("TP.csv", "a") as savedb:
w = csv.writer(savedb)
w.writerow([data])
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(),
current_date_time()])
e3.pack()
label1.pack()
b1.pack()
root.mainloop()
「####」に関しては、列サイズを拡張する必要があります。今はあなたのコードを読んでいるだけです。 – Goralight
また、あなたのcurrent_date_time'関数は、最後の2行をインデントしません。それは間違いですか? – Goralight
'%I'は12時間制を使用します。 24時間表示をしたい場合は '%H'を使います。あるいは、 '%I'と組み合わせて'%p'を使用してAM/PMインジケータを取得してください。 –