Pysimplegui: [Bug] Unable to clear value with value[key] = ''

Created on 8 Apr 2020  路  12Comments  路  Source: PySimpleGUI/PySimpleGUI

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

I'm assigning a path to a button (filebrowse), and after using it's value, I'm trying to delete it's value with values[button_key] = '', but it appears that the value wont change even though the code is runing ok.

This has worked before.

ps: I'm also reading the window with window.Read(timeout=2000)

Operating System

Windows 10

Python version

Python 3.8.1

PySimpleGUI Port and Version

PySimpleGUI 4.18.0.19

Most helpful comment

I think we can close this one again as you're on your way with a solution that seems to likely work. If it doesn't work out, then we can re-open again clearly.

All 12 comments

That's not how you modify elements.

This design pattern demonstrates updating items. All elements have an update method.

https://pysimplegui.readthedocs.io/en/latest/#pattern-2-b-persistent-window-multiple-reads-using-an-event-loop-updates-data-in-window

https://pysimplegui.readthedocs.io/en/latest/#updating-elements-changing-elements-values-in-an-active-window

Maybe I misunderstood, but

values[button_key]

is the return values dictionary. Any particular reason you want to modify it? Are you trying to simulate something?

Just to be clear, modifying it has no impact on elements themselves.

I'll explain what Im trying to do:

My program has 2 windows, the first one has this button that I configured to be a browse file:

Layout = [[sg.T("BANCO DE DADOS DE IM脫VEIS RURAIS\nGEORREFERENCIADOS", font="SEGOEUI 12 bold", justification="center")],
          [sg.B("CRIAR UM NOVO BANCO", key="cria", button_type=sg.BUTTON_TYPE_SAVEAS_FILE, file_types=(("Database", ".db"),)), sg.FileBrowse("CARREGAR UM BANCO", target="carrega", key="carrega", file_types=(("Database", ".db"),), change_submits=True, enable_events=True)],
          [sg.Exit("SAIR", button_color=("white", "red"))]]

The trick is in this sg.FileBrows tha I set key="carrega", as I wanted to make this button hold the value path for the file, I set the target as itself target="carrega":

image

The thing is, I only what the program to proceed if this file is the correct one, if the file is NOT correct, I need to erase this value to get out of the check file loop and make the user select another file:

    event, values = window.Read(timeout=2000)
    procede = False
    if event is None or event == 'SAIR':
        break
    elif values['cria'] != '': 
        #do some code
    elif values['carrega'] != '':
        while procede is False:
            Banco = values['carrega']
            conn = sqlite3.connect(Banco)
            cursor = conn.cursor()
            try:
                cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='Dados'")
                cursor.execute("""SELECT CADASTRADOR, CERTNASC_CONJ, CERTNASC_CONJ_NOME, CERTNASC_CONJ_TEM,
                CERTNASC_PROP, CERTNASC_PROP_NOME, CERTNASC_PROP_TEM, CNPJ_CONJ, CNPJ_CONJ_NOME, CNPJ_CONJ_TEM,
                CNPJ_PROP, CNPJ_PROP_NOME, CNPJ_PROP_TEM, CONTRATO, CONTRATO_NOME, CONTRATO_TEM, CPF_CONJ,
                CPF_CONJ_NOME, CPF_CONJ_TEM, CPF_PROP, CPF_PROP_NOME, CPF_PROP_TEM, DATA, DEC_POSSE,
                DEC_POSSE_NOME, DEC_POSSE_TEM, ENDERECO, ESCR_COMPRAVENDA, ESCR_COMPRAVENDA_NOME,
                ESCR_COMPRAVENDA_TEM, ESPOLIO, ESPOLIO_NOME, ESPOLIO_TEM, ID, INCRA, MUNICIPIO, NOME, RG, CPF, CNPJ,
                OET, PROFISSAO_CONJ, PROFISSAO_PROP, PROPRIEDADE, RECIBO_COMPRAVENDA, RECIBO_COMPRAVENDA_NOME,
                RECIBO_COMPRAVENDA_TEM, RG_CONJ, RG_CONJ_NOME, RG_CONJ_TEM, RG_PROP, RG_PROP_NOME, RG_PROP_TEM,
                RI, RI_NOME, RI_TEM, TEL, TEL2, TITULO_CONJ, TITULO_CONJ_NOME, TITULO_CONJ_TEM, TITULO_PROP,
                TITULO_PROP_NOME, TITULO_PROP_TEM FROM Dados""")
                procede = True
                conn.close()
            except:
                values['carrega'] = ''
                window.Refresh()
                sg.PopupError("ERRO: Banco de dados n茫o possui a estrutura correta.", title="ERRO!")
                break
    if procede is True:
        #do some code

If I manage to delete this value and set it to '' (nothing), it wont fall in this check file loop and the user will have the ability to choose another file or click in the other button, so I made this Try Except condition where if failed, the value of the button is deleted with values['carrega'] = '' and the code wont fall in this loop until the user select another file.

This example was working in past versions, and I use this type of thing through the entire code, but now the dict named "values " wont change even though values['carrega'] = '' DO erase the value (cuz if I try to print it, nothing is shown)

I'm sorry I misunderstood.

Maybe I'm still getting it wrong, sorry if I am.

I need to update documentation, clearly. I incorrectly said something about buttons being targets themselves which is no longer recommended.

This issue discusses something similar I think.

https://github.com/PySimpleGUI/PySimpleGUI/issues/2794

I haven't looked through all the release notes, but you may find something in there about buttons not returning stuff in the values dictionary now if it did before.

The right technique is to continue to target Input elements, but make them empty. If you want to immediately be told when a selection is made, turn on events. Otherwise wait for a button then check the values dictionary for the selected filename.

Thanks for your time, I'll change my code

Oh! That was it?

Damn, I need to change documentation. Can you give me some clues where you saw this button target technique? It's clearly somewhere and I don't want people to go through what you did.

I think this will work cuz I know that window[key].update() is properly working, so if I do this instead of trying to replace the button value, it might work. What I didn't know is that I can put a Input widget and make it invisible.

About where I found this technique, I saw in the declaration definition inside PySimpleGUI files. I often do this to know what parameters I can pass into a function.

... so I think thats my fault for trying to use things I should not. Sorry.

This Recipe may have helped?

https://pysimplegui.readthedocs.io/en/latest/cookbook/#recipe-get-filename-with-no-input-display-returns-when-file-selected

I was about to add one and discovered one already existed. What I need to do now is track down sources for this bad technique.

This "target" parameter is not referenced in the docs

https://pysimplegui.readthedocs.io/en/latest/#button-element

It's referenced extensively in the docs. There's an entire section called... Button Targets

image

I think we can close this one again as you're on your way with a solution that seems to likely work. If it doesn't work out, then we can re-open again clearly.

I DO need to remove the bit about the button being a target and instead suggest a hidden element be used.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scmanjarrez picture scmanjarrez  路  5Comments

ECOM-Klaus picture ECOM-Klaus  路  4Comments

mozesa picture mozesa  路  4Comments

MikeTheWatchGuy picture MikeTheWatchGuy  路  3Comments

ncotrb picture ncotrb  路  4Comments