Python/Tinker 'datetime.date' object has no attribute 'get' (Input date in to notepad doc)

0

I'm new.

To gain a idea of how basic coding works I have created an GUI where the end user will enter text in to a input widget, press a button and the text will display in the shell save on a notepad.

My GUI has three button;

  • Button 1 will save text entered in GUI to notepad and display via shell
  • Button 2 is to save today's date to the notepad and display via shell
  • Button 3 opens up the notepad .exe file

I am able to successfully save text in to the notepad and open the notepad via the GUI application. However, I am unable to save the date in the notepad.

This is an example of the code what displays the attribute error. (When button 2 is clicked)

 #030820, printing time  to shell and notepad # in progress
def button1Click():
    print (time.get)
    with open("Names101.txt", "a") as output:  #prints to notepad 
        output.write(time.get() + "\n") #prints to notepad
    
button1 = tk.Button(window, text=("Record Time"),width = 30, command=button1Click, bg="light blue")

This is the error displayed

File "C:\Users\shane\AppData\Local\Programs\Python\Python38- 
32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\shane\source\repos\GUI input to Notepad\GUI input to 
Notepad\GUI_input_to_Notepad.py", line 16, in button1Click
print (time.get)
AttributeError: 'datetime.date' object has no attribute 'get'
The thread 'MainThread' (0x1) has exited with code 0 (0x0).
The program 'python.exe' has exited with code -1 (0xffffffff).

This is the full code of my application

import tkinter as tk
import datetime
import os #opens a text file 
window = tk.Tk()
window.title ("Shane's Text to NotePad Testing GUI")
window.geometry("400x150")
window.configure(bg="#3BB9FF")

time = datetime.date.today()
print (time)


#030820, printing time  to shell and notepad # in progress
def button1Click():
    print (time.get)
    with open("Names101.txt", "a") as output:  #prints to notepad 
        output.write(time.get() + "\n") #prints to notepad
        

button1 = tk.Button(window, text=("Record Time"),width = 30, command=button1Click, bg="light blue")



#button3 opens txt file
def button3Click():
    os.system("Names101.txt")

button3 = tk.Button(window, text=("Open Notepad file"),width=30, command=button3Click, bg="light green")






#030820, printing entry1 to shell and notepad 

entry1 = tk.Entry(window, width = 50)

def entry1get():    
    print (entry1.get())    #prints to shell
    with open("Names101.txt", "a") as output:  #prints to notepad
        output.write(entry1.get() + "\n")   #prints to notepad
    
                

button2 = tk.Button(window, text="Input text to notepad", width=30, command=entry1get, bg="#E6E6FA")
    
    

#030820 Label one 


label1 = tk.Label(text="Enter text below", font="bold" , bg="#3BB9FF")   




label1.pack() 
entry1.pack() 
button2.pack() 
button1.pack() 
button3.pack()



window.mainloop()

`

question answered by Lewis Morris, below is resolution

def button1Click():
print (time.strftime("%Y-%m-%d"))
with open("Names101.txt", "a") as output:  #prints to notepad 
    output.write(time.strftime("%Y-%m-%d") + "\n")  #prints to notepad
    

button1 = tk.Button(window, text=("Record Time"),width = 30, 
command=button1Click, bg="light blue")  
python
tkinter
notepad
asked on Stack Overflow Aug 4, 2020 by Kekistan • edited Aug 4, 2020 by Kekistan

2 Answers

3

When printing dates you can use .strftime() method.

This allows you to print and format your datetime in any way you wish.

i.e

initialise your date.

time = datetime.date.today()

and then call strftime() with passed parameters such as

time.strftime("%Y-%m-%d")

printing should then be done as below

print(time.strftime("%Y-%m-%d"))

A full list of strftime codes is found here

https://www.programiz.com/python-programming/datetime/strftime

answered on Stack Overflow Aug 4, 2020 by Lewis Morris
0

The variable time is initialized as:

time = datetime.date.today()

As per the datetime docs, there is no attribute or class method that corresponds to your usage of time.get or time.get().

This change should fix your issue:

def button1Click():
    print (time)
    with open("Names101.txt", "a") as output:  #prints to notepad 
        output.write(str(time) + "\n") #prints to notepad
answered on Stack Overflow Aug 4, 2020 by coltonrusch

User contributions licensed under CC BY-SA 3.0