PySimpleGUI - Updating a listbox on a button event

Viewed 503

I'm trying to make a Python program with a UI that shows simple widget like a frame, a listbox and a table. I would like to populate the listbox with input from the user. So I create the listbox and "fill it" qith a blank list, then the user type in a name, click the button and the list will have that name in it and the name will be shown in the listbox. I quite manage to do this, but the name in the listbox are in vertical, and the element of the list are every single characters entered bu the user in the Input field. Furthermore, the elements will are overwritten when the user types another name in the inputbox.

This is the code

import PySimpleGUI as sg
from openpyxl import Workbook
from openpyxl import load_workbook

createColony_Layout=[]
firstWindow_Layout=[]
colonyList = []
# Software theme

# FIRST WINDOW
# Layouts
menuBar_Layout = [
                    ['&File', ['&Inserisci file ICM     Ctrl-O', '&Save       Ctrl-S', '&Properties', 'E&xit']],
                    ['&Edit', ['Undo']],
                    ['&Toolbar', ['---', 'Command &1::Command_Key', 'Command &2', '---', 'Command &3', 'Command &4']],
                    ['&Help', ['&About...']]
                 ]

createColony_Layout = [
                        [sg.Text('Insert researcher name', size=20)],
                        [sg.Input(size=15, key='c')],
                        [sg.Button(button_text='Create', button_type=7)]
                      ]

createColonyFrame = sg.Frame('Create new colony', createColony_Layout, size=(200, 100))

firstWindow_Layout = [
                        [sg.MenubarCustom(menuBar_Layout)],
                        [sg.Push(), sg.Text('Colony management',
                        justification=('Center'), font=('Helvetica', 30)), sg.Push()],
                        [createColonyFrame],
                        [sg.Listbox(colonyList, size =(50, 25), key='lista')]
                     ]

# Create window
window = sg.Window('Colony management', firstWindow_Layout, size=(1300, 700),
                   auto_size_text= True, resizable=True, finalize=True)

window.TKroot.minsize(500,250)
#window.TKroot.maxsize(600, 700)

# Program loop
while True:
        event,values = window.read()

        if event == 'Create':
            window['list'].update(values['c'])


        if event == sg.WINDOW_CLOSED:
           break

window.close()

And this is a screen of the window

I hope this is enough for you to help me, thank you.

1 Answers

The call to update for Listbox takes a list/tuple as the first argument. A string looks like a list of single characters in Python. To add a single value, make it a tuple instead of a string.

            window['list'].update(values['c'])

becomes

            window['lista'].update((values['c'],))

There is also a call to set the minimum size for a window that you can use instead of the TKroot access you've got in your code.

If you want a minimum size of (500, 250), then use this call:

window.set_min_size((500, 250))

import PySimpleGUI as sg
# from openpyxl import Workbook
# from openpyxl import load_workbook

createColony_Layout=[]
firstWindow_Layout=[]
colonyList = []
# Software theme

# FIRST WINDOW
# Layouts
menuBar_Layout = [
                    ['&File', ['&Inserisci file ICM     Ctrl-O', '&Save       Ctrl-S', '&Properties', 'E&xit']],
                    ['&Edit', ['Undo']],
                    ['&Toolbar', ['---', 'Command &1::Command_Key', 'Command &2', '---', 'Command &3', 'Command &4']],
                    ['&Help', ['&About...']]
                 ]

createColony_Layout = [
                        [sg.Text('Insert researcher name', size=20)],
                        [sg.Input(size=15, key='c')],
                        [sg.Button(button_text='Create', button_type=7)]
                      ]

createColonyFrame = sg.Frame('Create new colony', createColony_Layout, size=(200, 100))

firstWindow_Layout = [
                        [sg.MenubarCustom(menuBar_Layout)],
                        [sg.Push(), sg.Text('Colony management',
                        justification=('Center'), font=('Helvetica', 30)), sg.Push()],
                        [createColonyFrame],
                        [sg.Listbox(colonyList, size =(50, 25), key='lista')]
                     ]

# Create window
window = sg.Window('Colony management', firstWindow_Layout, size=(1300, 700),
                   auto_size_text= True, resizable=True, finalize=True)
window.set_min_size((500, 250))
# window.TKroot.minsize(500,250)
#window.TKroot.maxsize(600, 700)

# Program loop
while True:
        event,values = window.read()

        if event == 'Create':
            window['lista'].update((values['c'],))


        if event == sg.WINDOW_CLOSED:
           break

window.close()
Related