Search code examples
pythoncalendarpysimplegui

How to stop window.read() from firing again?


task_adding = [

    [
        pg.Text("Enter name of the task: ", size=(10, 1)),
        pg.InputText(key='-NAME-', do_not_clear=False)
    ],
    [
        pg.Text("Enter prio: "),
        pg.Slider(key='-PRIO-', range=(1,5), default_value=3, size=(20,15), orientation='horizontal')
    ],
    [
        pg.Text("Description:"),
        pg.Multiline(key='-DESC-', size=(30,10), do_not_clear=False)
    ],
    [
        pg.Input(key='-DATE-', size=(50, 1)),
        pg.CalendarButton("Datum zadatka", target='-DATE-', close_when_date_chosen=True, location=(50, 50), format='%Y-%m-%d', default_date_m_d_y=(int(date.today().strftime("%m")), int(date.today().strftime("%d")), int(date.today().strftime("%Y"))))
    ],
    [
        pg.Button("Add task")
    ]

]
while True:

    event, values = window.read()

    if event == pg.WIN_CLOSED:
        break

    elif event == "Add task":
        print(values)
        if (values['-DATE-'] < date.today().strftime('%Y-%m-%d')):
            pg.popup('Izabrali ste pogresan datum!')
        else:
            # Dodavanje taska u existing tasks
            tasks.addTask(t.Task(values['-NAME-'], values['-PRIO-'], values['-DESC-']))
            # Ovo je dodavanje reda u csv fajl
            with open('C:\\Users\\User\\Desktop\\Python\\ToDoList\\tasks.csv', 'a', newline='') as file:
                writer = csv.writer(file)
                writer.writerow([values['-NAME-'], values['-PRIO-'], values['-DESC-']])
            window['-TABLE-'].update(tasks.existingtasks) # Ovo je za update tabele

    elif event == "Ok":
        with open('C:\\Users\\User\\Desktop\\Python\\ToDoList\\sleeping_time.csv', 'a', newline='') as file:
            writer = csv.writer(file)
            writer.writerow([date.today(), str(values[0]) + ":" + str(values[1])])
        window.close()
        window = pg.Window("ToDoList", tab_group, size=(500,500))

    elif event == '-TABLE-':
        if values['-TABLE-']: # Ovo sam stavio da bismo videli da li je selektovano nesto iz tabele
            choice = pg.popup_yes_no("Da li ste zavrsili sa ovim taskom?") # popup sa YES NO
            if choice == "Yes":
                data_selected = [tasks.existingtasks[row] for row in values[event]] # selektovan red je niz od postojecih taskova od reda za red u values[event] sto predstavlja ustvari vrednost reda odnosno broj
                tasks.finishATask(data_selected[0][0]) # zavrsavamo task u tasks
                deleteRow(data_selected[0][0]) # brisemo task iz tasks.csv
                # Upisujemo task u finished_task.csv
                with open('C:\\Users\\User\\Desktop\\Python\\ToDoList\\finished_tasks.csv', 'a', newline='') as file:
                    writer = csv.writer(file)
                    task = tasks.finishedtasks[-1]
                    writer.writerow([datetime.now(), task[0], task[1], task[2]])

I have the main window with some input boxes and button for Calendar and also the main button to add a task. When I enter text in input boxes and click to choose a date my inputs refresh (I guess because the window.read() is called again since it opens another window for calendar). I want to keep do_not_clear=False for inputs so it fires when I press the main button but how to prevent it from happening when I press calendar button? If you need some more information please tell me, this is my first post.


Solution

  • IMO, there's no way to stop the clear action for the Input elements after calendarbutton clicked, you have to do the clear action by yourself, like

    import PySimpleGUI as sg
    
    layout = [
        [sg.Text("Name"),
         sg.Input(key='-NAME-')],
        [sg.Input(key='-DATE-', expand_x=True),
         sg.CalendarButton("Date")],
        [sg.Button("Add task")],
    ]
    window = sg.Window("Title", layout)
    
    while True:
    
        event, values = window.read()
    
        if event == sg.WIN_CLOSED:
            break
        elif event == "Add task":
            # DO the job to add task, then
            for key in ("-NAME-", "-DATE-"):
                window[key].update("")
    
    window.close()