Home > other >  How do I get a RadioButton to switch values when I use FileBrowse (to the option associated with the
How do I get a RadioButton to switch values when I use FileBrowse (to the option associated with the

Time:12-20

There's an input section where the user selects a file. The default value is the first radio button (a simple file path string) and the "Use specified file" is the second radio button. I would like to have the radio button selection jump down from the first (default) option to the second (specified) option after the user uses the FileBrowser option. My code is this so far:

import PySimpleGUI as sg
from os.path import isfile, join, getctime
import numpy as np

checkboxes = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

type_2 = (
    ("Files", "*.txt *.log"),
)

layout = [[sg.Text('Welcome\n\n')],
          [sg.Radio('\tUse default file', "RADIO1", enable_events=True, key='R1', default=True)],
          [sg.Radio('\tUse specified file', "RADIO1", enable_events=True, key='R2', default=False)],
          [sg.Text("Choose a file: "), sg.FileBrowse(file_types=type_2)],

          [sg.Checkbox('\t ' checkboxes[0], default=True)],
          [sg.Checkbox('\t ' checkboxes[1], default=True)],
          [sg.Checkbox('\t ' checkboxes[2], default=True)],
          [sg.Checkbox('\t ' checkboxes[3], default=True)],
          [sg.Checkbox('\t ' checkboxes[4], default=True)],
          [sg.Checkbox('\t ' checkboxes[5], default=True)],
          [sg.Checkbox('\t ' checkboxes[6], default=True)],
          [sg.Button('Go')], [sg.Button('Cancel')]]

window = sg.Window(title="A Title", layout=layout, margins=(150,40))

while True:
    event, values = window.read()
    if event == 'Cancel' or event == sg.WIN_CLOSED:
        quit()
        break
    if event == 'Go':
        break

I've looked into the Update, i.e.:

window.Element('r2').Update(value=True)

documentation, but not sure if I can use it for this situation. Please help let me know.

CodePudding user response:

Add the options enable_event=True and key='Browse' for the Button sg.FileBrowse and set the value to True for the Radio element when event 'Browse'.

...
          [sg.Text("Choose a file: "), sg.FileBrowse(enable_events=True, target='Browse', key='Browse')],
...
    if event == 'Browse':
        window['R2'].update(value=True)
...
  • Related