-2

I'm creating a program that gets a date from users and now I need to convert to string 'cause I need to put this data into a command shell that I'm implementing, with tkinter and tkcalendar (that lib provides me to get entry). How can I do that?

Here the code that I'm building to run on git:

cal = Calendar(top, font='Arial 14', selectmode='day', cursor='hand1')
subprocess.call('git log --pretty='"format: % h"' --after=' +cal+' --before='+cal, shell=True)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Does this answer your question? [Convert datetime object to a String of date only in Python](https://stackoverflow.com/questions/10624937/convert-datetime-object-to-a-string-of-date-only-in-python) – wjandrea Mar 02 '20 at 21:24
  • @wjandrea I tried and this was the exception from terminal: ```Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "window.py", line 29, in calendar cal.strtime('%Y/%m/%d')+' --before='+cal.strtime('%Y/%m/%d'), shell=True) AttributeError: 'Calendar' object has no attribute 'strtime'``` – Igor Pompeo Mar 02 '20 at 21:32
  • You need to use the `datetime` attribute of the `Calendar` object, and then it's `strftime`, not `strtime` – wjandrea Mar 02 '20 at 21:39
  • Sorry about that but, looks that: ```Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "window.py", line 28, in calendar subprocess.call('git log --pretty='"format: % h"' --after=' + cal.datetime.strftime( TypeError: descriptor 'strftime' for 'datetime.date' objects doesn't apply to a 'str' object``` – Igor Pompeo Mar 02 '20 at 21:41

1 Answers1

0

In your case cal is a Calendar instance and cal.datetime returns python's datetime object which has strftime to format datetime to string:

dt = cal.datetime.strftime("%Y-%m-%d")
subprocess.call(f"""git log --pretty="format: %h" --after={dt} --before={dt}""", shell=True)
frost-nzcr4
  • 1,540
  • 11
  • 16