I
I
i_go_hard2021-11-02 13:01:29
Python
i_go_hard, 2021-11-02 13:01:29

How to update first window table with button from second window using PySimpleGUI library in Python?

Hello. Trying to saddle the PySimpleGUI library, I ran into such a problem: I want to add a new row to the array and then update the table on the click of a button, where the entire array is displayed, but I can’t figure out the keys and the Update function to update the table. Source:

import PySimpleGUI as sg
import numpy as np
import pandas as pd
df = pd.read_csv("filename.csv",delimiter=',')
tab = np.array(df).tolist()
headings = ['Name','DOB','PhoneNumber','Position']
sg.theme('Dark Green')
def make_win1():
    layout = [
        [sg.FileBrowse('Выбрать базу данных'),
         sg.Button('Добавить сотрудника',size=(20,1)),
         sg.Button('Удалить сотрудника',size=(20,1)),
         sg.Button('Изменить данные сотрудника',size=(25,1))],
        [sg.Table(values=tab,
                    headings=headings,
                    max_col_width=35,
                    auto_size_columns=True,
                    display_row_numbers=True,
                    justification='center',
                    alternating_row_color='lightyellow',
                    key='-TABLE-',
                    size=(100,20),
                    enable_events=True,
                    row_height=30)],
    ]
    return sg.Window('Window Title', layout,finalize=True,grab_anywhere=True)
def make_win2():
    l_col = sg.Column(
        [
        [sg.Text('Фио'),sg.Input(key='-IN-1', enable_events=True)],
        [sg.Text('Дата рождения'),sg.Input(key='-IN-2', enable_events=True)],
        [sg.Text('Номер телефона'),sg.Input(key='-IN-3', enable_events=True)],
        [sg.Text('Должность'),sg.Input(key='-IN-4', enable_events=True)],
        [sg.Button('Добавить')]
        ],
        element_justification="right"    
)
    layout = [
        [l_col]
        ]
    return sg.Window('Second Window', layout, finalize=True)
window1, window2 = make_win1(), None        # start off with 1 window open
while True:             # Event Loop
    window, event, values = sg.read_all_windows() # Read the event that happened and the values dictionary
    if event == sg.WIN_CLOSED or event == 'Exit': # If user closed window with X or if user clicked "Exit" button then exit
        window.close()
        if window == window2:       # if closing win 2, mark as closed
            window2 = None
        elif window == window1:     # if closing win 1, exit program
            break
    elif event == 'Добавить сотрудника' and not window2:
        window2 = make_win2()
    elif event == 'Добавить':
        new_line={'Name':"-IN-1",'DOB':'-IN-2','PN':'-IN-3','Pos':'-IN-4'}
        df=df.append(new_line,ignore_index=True)
        window1['-TABLE-'].Update(values['-IN-1','-IN-2','-IN-3','-IN-4'])
        #print(df)
window.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2021-11-02
@HemulGM

It's not a problem, not a desire to learn the library. Why don't you go freelancing and let someone who knows solve the problem?
PS In programming, this often happens: you have to study some kind of library before writing something on it. Not fair, I agree.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question