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

[Bug] Extract data from Column

VladAdGad opened this issue · comments

Type of Issue (Enhancement, Error, Bug, Question)

Bug


Operating System

Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter


Versions

Python version: 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
port: tkinter
tkinter version: 8.6.13
PySimpleGUI version: 4.60.5
PySimpleGUI filename: /root/.cache/pypoetry/virtualenvs/fcompress-9TtSrW0h-py3.11/lib/python3.11/site-packages/PySimpleGUI/PySimpleGUI.py


Your Experience In Months or Years (optional)

Years Python programming experience
1

Years Programming experience overall
6

Have used another Python GUI Framework? (tkinter, Qt, etc) (yes/no is fine)
yes

Anything else you think would be helpful?
no


Troubleshooting

These items may solve your problem. Please check those you've done by changing - [ ] to - [X]

  • Searched main docs for your problem www.PySimpleGUI.org
  • Looked for Demo Programs that are similar to your goal. It is recommend you use the Demo Browser! Demos.PySimpleGUI.org
  • None of your GUI code was generated by an AI algorithm like GPT
  • If not tkinter - looked for Demo Programs for specific port
  • For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • Run your program outside of your debugger (from a command line)
  • Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • Have upgraded to the latest release of PySimpleGUI on PyPI (lastest official version)
  • Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

Detailed Description

I'm trying to fetch/get data from Column and iterate through the elements. As a GUI representation everything works fine but behind the scenes I cannot extract data.
I tried to
Column.Rows
Column.ReturnValues
Column.ReturnValuesList
Column.ReturnValuesDictionary

and every of these value is empty.

So I look into source code and found out that extend_layout() has a line
self.AddRow(column)
not a container.AddRow(rows)
or smth like this.

As I understood there are no usage of Rows for Column itself.

I purposely don't want to store data on my own such as files = [] and on event update this list either adding or removing the element.

Of course I can search by keys. Like window.find_element('-ADD-FILE-0')
window.find_element('-ADD-FILE-1')
window.find_element('-ADD-FILE-2')
window.find_element('-ADD-FILE-3')

these numbers are generated automatically but I think it is a bad code because I need to try catch an exception if key not found - then I'm at the end of list. It would be nice if your function returned None or Empty instead of rising exception. (I found out later that you have silent option, I already checked it and still I'd suggest returning an empty value instead of ErrorElement)

My question is how to get list of Inputs from Column in your opinion?
Why and what Rows does do in Column then?

Code To Duplicate

import PySimpleGUI as psg

layout = [[psg.Column([[]], scrollable=True, vertical_scroll_only=True, key='-FILES-', expand_x=True, expand_y=True, pad=(0, 0))]]
psg.Window('YAGC', layout, resizable=True, auto_size_buttons=True, auto_size_text=True, size=(800, 400), finalize=True)

files: psg.Column = window.find_element('-FILES-)

while True:
  event, values = window.read()
  if event == GUIKey.ADD_FILE:
    window.extend_layout(
      files,
      [[
        psg.Input(default_text=values['-ADD-FILE-'], key='-ADD-FILE-', readonly=True, expand_x=True),
        psg.FileBrowse()
      ]]
    )
    window.visibility_changed()
    files.contents_changed()
  window.refresh()
  files.contents_changed()

  print(files.Rows) #is empty

Screenshot, Sketch, or Drawing

Screenshot from 2024-02-01 11-02-41


Watcha Makin?

I'm doing a GUI for my family for compression so they can reduce size of videos and images :)

It would be nice if your function returned None or Empty instead of rising exception.

find_element has parameters to control exceptions and error conditions. See the call reference for more on this:

https://www.pysimplegui.org/en/latest/call%20reference/#find_element

Here's the Docstring that's used to generate the call reference:

    def find_element(self, key, silent_on_error=False, supress_guessing=None, supress_raise=None):
        """
        Find element object associated with the provided key.
        THIS METHOD IS NO LONGER NEEDED to be called by the user

        You can perform the same operation by writing this statement:
        element = window[key]

        You can drop the entire "find_element" function name and use [ ] instead.

        However, if you wish to perform a lookup without error checking, and don't have error popups turned
        off globally, you'll need to make this call so that you can disable error checks on this call.

        find_element is typically used in combination with a call to element's update method (or any other element method!):
        window[key].update(new_value)

        Versus the "old way"
        window.FindElement(key).Update(new_value)

        This call can be abbreviated to any of these:
        find_element = FindElement == Element == Find
        With find_element being the PEP8 compliant call that should be used.

        Rememeber that this call will return None if no match is found which may cause your code to crash if not
        checked for.

        :param key:              Used with window.find_element and with return values to uniquely identify this element
        :type key:               str | int | tuple | object
        :param silent_on_error:  If True do not display popup nor print warning of key errors
        :type silent_on_error:   (bool)
        :param supress_guessing: Override for the global key guessing setting.
        :type supress_guessing:  (bool | None)
        :param supress_raise:    Override for the global setting that determines if a key error should raise an exception
        :type supress_raise:     (bool | None)
        :return:                 Return value can be: the Element that matches the supplied key if found; an Error Element if silent_on_error is False; None if silent_on_error True
        :rtype:                  Element | ErrorElement | None
        """

The FedEx Package Tracking Demo Program uses the silent_on_error parameter if you want to see an example use.

I'd suggest returning an empty value

This will return None if the key is not found

window.find_element('my key', supress_guessing=True, supress_raise=True, silent_on_error=True)

I got an idea from your example.

gui_files.add(values[GUIKeys.ADD_FILE])
window.metadata['file_id'] += 1

But I've met another problem related with this issue #5486. I put a comment there but it is still as closed. Should I create a new issue?

Should I create a new issue?

Yes please

image