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.
https://www.PySimpleGUI.com
Other
13.36k stars 1.84k forks source link

[Question] NOTE - Table get doesn't work #6750

Closed testxx22 closed 4 months ago

testxx22 commented 4 months ago

Hi , i want to get values on Table.Older versions i use window['table'].get() but this version i can't use. This function returns only selected row index how can i get all values ?


Operating System

windows 11

PySimpleGUI Port (tkinter, Qt, Wx, Web)

PySimpleGUI 5.0.4


Versions

PySimpleGUI 5.0.4

Python version (sg.sys.version)

3.11

PySimpleGUI Version (sg.__version__)

PySimpleGUI 5.0.4

GUI Version (tkinter (sg.tclversion_detailed), PySide2, WxPython, Remi)

PySimpleGUI 5.0.4

jason990420 commented 4 months ago

get()

Get the selected rows using tktiner's selection method. Returns a list of the selected rows.

Basically, you should know what all the data in a Table element when you initiate a Table element.

testxx22 commented 4 months ago

get() Get the selected rows using tktiner's selection method. Returns a list of the selected rows.

Basically, you should know what all the data in a Table element when you initiate a Table element.

Yes but in the procsses i change some datas so i want to get all the variables but i can't now

jason990420 commented 4 months ago

You should update your data at the same time when you change some datas, or

from pprint import pprint
import PySimpleGUI as sg

information = [
    ['President',           'Born',       'Died',       'Age'],
    ['George Washington',   '02/22/1732', '12/14/1799', 67],
    ['John Adams',          '10/30/1735', '07/04/1826', 90],
    ['Thomas Jefferson',    '04/13/1743', '07/04/1826', 83],
    ['James Madison',       '03/16/1751', '06/28/1836', 85],
    ['James Monroe',        '04/28/1758', '07/04/1831', 73],
    ['John Quincy Adams',   '07/11/1767', '02/23/1848', 80],
    ['Andrew Jackson',      '03/15/1767', '06/08/1845', 78],
    ['Martin Van Buren',    '12/05/1782', '07/24/1862', 79],
    ['William Harrison',    '02/09/1773', '04/04/1841', 68],
    ['John Tyler',          '03/29/1790', '01/18/1862', 71],
    ['James Polk',          '11/02/1795', '06/15/1849', 53],
    ['Zachary Taylor',      '11/24/1784', '07/09/1850', 65],
    ['Millard Fillmore',    '01/07/1800', '03/08/1874', 74],
    ['Franklin Pierce',     '11/23/1804', '10/08/1869', 64],
    ['James Buchanan',      '04/23/1791', '06/01/1868', 77],
    ['Abraham Lincoln',     '02/12/1809', '04/15/1865', 56],
    ['Andrew Johnson',      '12/29/1808', '07/31/1875', 66],
    ['Ulysses S. Grant',    '04/27/1822', '07/23/1885', 63],
    ['Rutherford B. Hayes', '10/04/1822', '01/17/1893', 70],
    ['James Garfield',      '11/19/1831', '09/19/1881', 49],
    ['Chester A. Arthur',   '10/05/1829', '11/18/1886', 56],
    ['Grover Cleveland',    '03/18/1837', '06/24/1908', 71],
    ['Benjamin Harrison',   '08/20/1833', '03/13/1901', 67],
    ['Grover Cleveland',    '03/18/1837', '06/24/1908', 71],
    ['William McKinley',    '01/29/1843', '09/14/1901', 58],
    ['Theodore Roosevelt',  '10/27/1858', '01/06/1919', 60],
    ['William Taft',        '09/15/1857', '03/08/1930', 72],
    ['Woodrow Wilson',      '12/28/1856', '02/03/1924', 67],
    ['Warren Harding',      '11/02/1865', '08/02/1923', 57],
    ['Calvin Coolidge',     '07/04/1872', '01/05/1933', 60],
    ['Herbert Hoover',      '08/10/1874', '10/20/1964', 90],
    ['Franklin Roosevelt',  '01/30/1882', '04/12/1945', 63],
    ['Harry Truman',        '05/08/1884', '12/26/1972', 88],
    ['Dwight Eisenhower',   '10/14/1890', '03/28/1969', 78],
    ['John F. Kennedy',     '05/29/1917', '11/22/1963', 46],
    ['Lyndon Johnson',      '08/27/1908', '01/22/1973', 64],
    ['Richard Nixon',       '01/09/1913', '04/22/1994', 81],
    ['Gerald Ford',         '07/14/1913', '12/20/2006', 93],
    ['Ronald Reagan',       '02/06/1911', '06/05/2004', 93],
    ['George H.W. Bush',    '06/12/1924', '11/30/2018', 94],
]
headings = information[0]
data = information[1:]
widths = list(map(max, map(lambda x: map(lambda y:len(str(y))+2, x), zip(*information))))

sg.set_options(font=("Courier New", 12))
layout = [
    [sg.Table(
        data,
        headings=headings,
        auto_size_columns=False,
        col_widths=widths,
        cols_justification='lccc',
        key='TABLE')],
    [sg.Button("Get")],
]
window = sg.Window("Table", layout, margins=(0, 0), finalize=True)
tree = window["TABLE"].widget

while True:

    event, values = window.read()

    if event == sg.WINDOW_CLOSED:
        break

    elif event == "Get":
        table_data = [[value for value in tree.item(line)['values']] for line in tree.get_children()]
        pprint(table_data)

window.close()
[['George Washington', '02/22/1732', '12/14/1799', 67],
 ['John Adams', '10/30/1735', '07/04/1826', 90],
 ['Thomas Jefferson', '04/13/1743', '07/04/1826', 83],
 ['James Madison', '03/16/1751', '06/28/1836', 85],
 ['James Monroe', '04/28/1758', '07/04/1831', 73],
 ['John Quincy Adams', '07/11/1767', '02/23/1848', 80],
 ['Andrew Jackson', '03/15/1767', '06/08/1845', 78],
 ['Martin Van Buren', '12/05/1782', '07/24/1862', 79],
 ['William Harrison', '02/09/1773', '04/04/1841', 68],
 ['John Tyler', '03/29/1790', '01/18/1862', 71],
 ['James Polk', '11/02/1795', '06/15/1849', 53],
 ['Zachary Taylor', '11/24/1784', '07/09/1850', 65],
 ['Millard Fillmore', '01/07/1800', '03/08/1874', 74],
 ['Franklin Pierce', '11/23/1804', '10/08/1869', 64],
 ['James Buchanan', '04/23/1791', '06/01/1868', 77],
 ['Abraham Lincoln', '02/12/1809', '04/15/1865', 56],
 ['Andrew Johnson', '12/29/1808', '07/31/1875', 66],
 ['Ulysses S. Grant', '04/27/1822', '07/23/1885', 63],
 ['Rutherford B. Hayes', '10/04/1822', '01/17/1893', 70],
 ['James Garfield', '11/19/1831', '09/19/1881', 49],
 ['Chester A. Arthur', '10/05/1829', '11/18/1886', 56],
 ['Grover Cleveland', '03/18/1837', '06/24/1908', 71],
 ['Benjamin Harrison', '08/20/1833', '03/13/1901', 67],
 ['Grover Cleveland', '03/18/1837', '06/24/1908', 71],
 ['William McKinley', '01/29/1843', '09/14/1901', 58],
 ['Theodore Roosevelt', '10/27/1858', '01/06/1919', 60],
 ['William Taft', '09/15/1857', '03/08/1930', 72],
 ['Woodrow Wilson', '12/28/1856', '02/03/1924', 67],
 ['Warren Harding', '11/02/1865', '08/02/1923', 57],
 ['Calvin Coolidge', '07/04/1872', '01/05/1933', 60],
 ['Herbert Hoover', '08/10/1874', '10/20/1964', 90],
 ['Franklin Roosevelt', '01/30/1882', '04/12/1945', 63],
 ['Harry Truman', '05/08/1884', '12/26/1972', 88],
 ['Dwight Eisenhower', '10/14/1890', '03/28/1969', 78],
 ['John F. Kennedy', '05/29/1917', '11/22/1963', 46],
 ['Lyndon Johnson', '08/27/1908', '01/22/1973', 64],
 ['Richard Nixon', '01/09/1913', '04/22/1994', 81],
 ['Gerald Ford', '07/14/1913', '12/20/2006', 93],
 ['Ronald Reagan', '02/06/1911', '06/05/2004', 93],
 ['George H.W. Bush', '06/12/1924', '11/30/2018', 94]]
PySimpleGUI commented 4 months ago

The member variable Table.Values returns the value of the table. If it's updated via Table.update, then the same member variable, Table.Values is changed to point to the new data.

It probably would be a good idea to add a property to the Table element instead of directly accessing this variable.

PySimpleGUI commented 4 months ago

Note that the Table element does not make a copy of the data provided when it's created or if the update method is used to modify the table. This means if you modify your table in your code, it will also modify the values that are shown in the table itself.

PySimpleGUI commented 4 months ago

Older versions i use window['table'].get() but this version i can't use. This function returns only selected row index

For the Table element, the get method has always returned the selected rows. The get method for elements is used to get the value that would normally be in the values dictionary. For a Table, the values dictionary contains a list of the selected rows.

You can also manually keep a copy of your table in the element's metadata.