2

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 Answers1

0

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()
Mike from PSG
  • 5,312
  • 21
  • 39
  • Thanks alot for the reply. I have tried this method but every inputs override the last one, I would like to "stack" every inputs the user types in the listbox. BTW thanks for the set_min_size() method! :D – NyctalusNoctula May 20 '22 at 16:03
  • You need to keep track of the list of items and then pass that in. I was solving the problem of the bug in your code, not writing the remainder of the program.... sorry I didn't realize it was add... I saw "create". Make a list, pass in the list. – Mike from PSG May 20 '22 at 19:29