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

How about

now = str(datetime.datetime.now())

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()

import datetime

self.textarea.insert(END,f"{datetime.datetime.now}")
textarea.insert(END,f"{datetime.datetime.now()}")

commented: Why 10 years later? -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.