Home > Blockchain >  Input multiple dates into PySimpleGUI Multiline box
Input multiple dates into PySimpleGUI Multiline box

Time:04-14

I would like to be able to paste a list of dates from Excel into a PySimpleGUI multiline box and save the entry as a pandas DateTime list.

This is my current code:

layout = [
    [sg.Text("Please fill out the following fields:", font=font)],
    [
        sg.Text("Date List", size=size, font=font),
        sg.Multiline(size=(20, 10), font=font, key="date_list"),
    ],
    [
        sg.Submit(font=font),
        sg.Button("Clear", font=font),
        sg.Exit(font=font),
    ],
]
window = sg.Window("Date list entry form", layout)

This is what I copy from Excel:

enter image description here

This is the output:

('Submit', {'date_list': '3/18/2020\n3/19/2021\n3/18/2022'})

How do I convert date_list to a pandas DateTime list?

CodePudding user response:

Call method str.splitlines to values['date_list'], and iterate items by method pd.Timestamp.

import pandas as pd
import PySimpleGUI as sg

font = ('Courier New', 11)

layout = [
    [sg.Text("Please fill out the following fields:", font=font)],
    [
        sg.Text("Date List", font=font),
        sg.Multiline(size=(20, 10), font=font, key="date_list"),
    ],
    [
        sg.Submit(font=font),
        sg.Button("Clear", font=font),
        sg.Exit(font=font),
    ],
]
window = sg.Window('Title', layout, finalize=True)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'Submit':
        lst = values['date_list'].strip().splitlines()
        try:
            print(list(map(pd.Timestamp, lst)))
        except ValueError:
            print('Wrong datetime format')

window.close()
[Timestamp('2020-03-18 00:00:00'), Timestamp('2021-03-19 00:00:00'), Timestamp('2022-03-18 00:00:00')]
  • Related