Search code examples
pythontkinterpython-datetime

I'm going to make a notepad using tkinter in Python


def func_time() :
   with open ('filename.txt' ,'w') as File:
       time = datetime.now()
       time_string = time.strftime('%m/%d/%Y')
       File.write(time_string)

If you press the Time/Date button like a Windows internal notepad, I want the current time and date to be written on the notepad.


Solution

  • Menu item not added for time/date.

    editMenu = Menu(mainMenu)
    mainMenu.add_cascade(label = "datetime", menu = editMenu)      # Item added
    editMenu.add_command(label = "time/date", command = func_time)
    

    The callback revised to insert datetime into the position of INSERT.

    def func_time() :
        time_string = datetime.now().strftime('%m/%d/%Y')
        st.insert(tk.INSERT, time_string)