Hello everyone,
I am writing a program that has a part where the user can type in notes,
and I would like the program to insert the date and time into the beginning
of the note.

I have the time and date part figured out:

import datetime

now = datetime.datetime.now()

print(now)

But I haven't been able to figure out how to insert it into the Text(tkinter)
I am using for the user to write the note in.

Any advice in this area would help me exponentially!

Thanks you guys,

-WolfShield

From there how do I transfer the string with the date and time
into, say, an entry I have, or a Text widget in my program?

-WolfShield

Here small code which insert each time the button is pressed clears the text and put date and time string to Entry

import Tkinter as tk
import datetime

def dt_ins():
    e.delete(0, tk.END)
    e.insert(0, str(datetime.datetime.now()))

root = tk.Tk()
root.title("Datetime Test")
root.geometry('240x60+200+200')
e = tk.Entry(root)
b = tk.Button(root, text="Test!", command=dt_ins)
e.pack()
b.pack()
root.mainloop()

I would prefer to use time module:

import Tkinter as tk
import time

def dt_ins():
    e.delete(0, tk.END)
    e.insert(0, time.asctime())

root = tk.Tk()
root.title("time test")
root.geometry('240x60+200+200')
e = tk.Entry(root)
b = tk.Button(root, text="Test!", command=dt_ins)
e.pack(fill= tk.BOTH, expand = tk.YES)
b.pack()
root.mainloop()

Edited 6 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.