PySimpleGUI / PySimpleGUI

Python GUIs for Humans! PySimpleGUI is the top-rated Python application development environment. Launched in 2018 and actively developed, maintained, and supported in 2024. Transforms tkinter, Qt, WxPython, and Remi into a simple, intuitive, and fun experience for both hobbyists and expert users.

Home Page:https://www.PySimpleGUI.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: .append Deprecated how to store values using .concat

squicciarini16 opened this issue · comments

Windows 11 OS
PySimpleGUI == 4.59.0
Python == 3.9
Pandas == 1.42.

understandingTheCodeWhileLoop

I'm currently building a questionnaire for a non profit organization and having issues updating the .append method to a .concat. I've attempted to turn off future warnings. My goal is to store the values in an Excel spreadsheet. Open for any solution to store the values to a spreadsheet

It looks like this issue is not about PySimpleGUI.

Example Code

from pathlib import Path
import pandas as pd
import PySimpleGUI as sg

EXCEL_FILE = r'd:\Example.xls'
items = ['Date', 'Item', 'Price']
df1 = pd.read_excel(EXCEL_FILE, index_col=0) if Path(EXCEL_FILE).is_file() else pd.DataFrame()

layout = [
    [sg.Text(item), sg.Push(), sg.Input('', do_not_clear=False, key=item)]
        for item in items] + [
    [sg.Button('Submit'), sg.Button('Exit')],
]
window = sg.Window('Accounting Book', layout)

while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    elif event == 'Submit':
        if '' not in values.values():
            try:
                values['Price'] = float(values['Price'])
            except ValueError:
                continue
            df2 = pd.DataFrame([values])
            df1 = pd.concat([df1, df2], ignore_index=True)
            df1.to_excel(EXCEL_FILE, index=True)

window.close()

Yea... a head-scratcher.

There is one demo program that imports pandas

https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Table_Pandas.py

Here's the code... it doesn't have append anywhere in it

#!/usr/bin/env python
import PySimpleGUI as sg
import pandas as pd

# Yet another example of showing CSV data in Table

def table_example():

    sg.set_options(auto_size_buttons=True)
    filename = sg.popup_get_file(
        'filename to open', no_window=True, file_types=(("CSV Files", "*.csv"),))
    # --- populate table with file contents --- #
    if filename == '':
        return

    data = []
    header_list = []
    button = sg.popup_yes_no('Does this file have column names already?')

    if filename is not None:
        try:
            # Header=None means you directly pass the columns names to the dataframe
            df = pd.read_csv(filename, sep=',', engine='python', header=None)
            data = df.values.tolist()               # read everything else into a list of rows
            if button == 'Yes':                     # Press if you named your columns in the csv
                # Uses the first row (which should be column names) as columns names
                header_list = df.iloc[0].tolist()
                # Drops the first row in the table (otherwise the header names and the first row will be the same)
                data = df[1:].values.tolist()
            elif button == 'No':                    # Press if you didn't name the columns in the csv
                # Creates columns names for each column ('column0', 'column1', etc)
                header_list = ['column' + str(x) for x in range(len(data[0]))]
        except:
            sg.popup_error('Error reading file')
            return

    layout = [
        [sg.Table(values=data,
                  headings=header_list,
                  display_row_numbers=True,
                  auto_size_columns=False,
                  num_rows=min(25, len(data)))]
    ]

    window = sg.Window('Table', layout, grab_anywhere=False)
    event, values = window.read()
    window.close()


table_example()

I'm struggling to find any connection to PySimpleGUI.