Richard E
Richard E

Reputation: 384

How to generate a column of checkboxes from a list

I recently found PySimpleGUI. At the time looked like it could save me a lot of work.

I wanting to create a 'tagger' for jpeg images using PySimpleGUI as the interface.

I have a long list of tag-words in a simple python list.

I have had some success as I can create a row of checkboxes, but not a column.

My initial research found a generator to use to make many checkboxes.
My goal is to have 3 columns filled with list generated chackboxes.

Generator Snippet

I found this snippet of code to generate the buttons from this PySimpleGUI page. Then modified for checkboxes. I have working functions, but I can't format the construct them to a column.

def CBtn(BoxText):
    return sg.Checkbox(BoxText, size=(8, 1), default=False)

column2 = sg.Column([[sg.Text('User Id:')], [CBtn(Bx) for Bx in Tags1]])

Main Code


#!/usr/bin/env python3
import PySimpleGUI as sg

sg.theme('Dark Red')
TaggerList = ["viking", "saddle", "beast", "ze", "princess", "vet", "art", "two", "hood", "mosaic",
              "viking1", "saddle1", "beast1", "ze1", "princess1", "vet1", "art1", "two1", "hood1", "mosaic1"]

TaggerListLen = len(TaggerList)
Tags1 = TaggerList[:int(TaggerListLen/3)]
Tags2 = TaggerList[int(TaggerListLen/3):int(TaggerListLen/3*2)]
Tags3 = TaggerList[int(TaggerListLen/3*2):]


def CBtn(BoxText):
    return sg.Checkbox(BoxText, size=(8, 1), default=False)

column2 = [[sg.Text('Column 2', justification='center', size=(10, 1))], [CBtn(Bx) for Bx in Tags2]]

column5 = sg.Column([[sg.Checkbox("BoxText1", size=(8, 1), default=False)],
            [sg.Checkbox("BoxText2", size=(8, 1), default=False)],
            [sg.Checkbox("BoxText3", size=(8, 1), default=False)],
            [sg.Checkbox("BoxText4", size=(8, 1), default=False)]])

layout = [
    [sg.Menu(menu_def, tearoff=True)],
    [sg.Text('Image Tagger', size=(
        30, 1), justification='center', font=("Helvetica", 25), relief=sg.RELIEF_RIDGE)],
    [sg.Text('Your Folder', size=(15, 1), justification='right'),
        sg.InputText('Default Folder'), sg.FolderBrowse()],
    [sg.Column(column2)]
    [column5]

window = sg.Window('Everything bagel', layout)

Shown in the screen capture the generated checkboxes are set in a row and not a column. So sg.Column(column2) is a row. When I manually add the checkboxes for column5 I get a proper column.

Screen_Capture

Format

When I use the generator definition I get this: [CBtn('1'), CBtn('2'), CBtn('3'), CBtn('log'), CBtn('ln'), CBtn('-')],

I'm looking for something like this: [[CBtn('1')], [CBtn('2')], [CBtn('3')], [CBtn('log')], [CBtn('ln')], [CBtn('-')]], As this follows the manual column5 format.

I've have tried many variations of the generator and def without much success. Here is an example of my attempts to achieve the format above.

column1 = [[sg.Text('Column 1', justification='center', size=(10, 1))], [BaseTag.append([CBtn(Bx)]) for Bx in Tags1]]

But I get a AttributeError: 'NoneType' object has no attribute 'ParentContainer'

Final

This was suppose to be easy. I guess, I'm bending the framework too much. PySimplegui looked good, but after hours of no head way, I wonder.

Upvotes: 0

Views: 2513

Answers (2)

Richard E
Richard E

Reputation: 384

This almost works. I needed to update the generator slightly to get what I wanted. The column gernated only had numbers. I needed to add the keywords.

Modified code:

column1 = [[CBtn(Tags1[i])] for i in range(len(Tags1))]

Then the column is populated with the values from the list. Now on to how more easy this frame is.

Upvotes: 1

r-beginners
r-beginners

Reputation: 35135

As far as I can tell, mass production of objects can only be done in a simple loop. Modifications to the code:. 1. [[CBtn(i)] for i in range(len(Tags2))] 2. 'column2' and 'column5' were changed to 'column2' and 'column5' respectively. 3. added in 'layout' as there are no commas or brackets; column2)], column5]]

def CBtn(BoxText):
    return sg.Checkbox(BoxText, size=(8, 1), default=False)

col2 = [[CBtn(i)] for i in range(len(Tags2))]

col5 = sg.Column([[sg.Checkbox("BoxText1", size=(8, 1), default=False)],
            [sg.Checkbox("BoxText2", size=(8, 1), default=False)],
            [sg.Checkbox("BoxText3", size=(8, 1), default=False)],
            [sg.Checkbox("BoxText4", size=(8, 1), default=False)]])

layout = [[sg.Menu(menu_def, tearoff=True)],
    [sg.Text('Image Tagger', size=(
        30, 1), justification='center', font=("Helvetica", 25), relief=sg.RELIEF_RIDGE)],
    [sg.Text('Your Folder', size=(15, 1), justification='right'),
        sg.InputText('Default Folder'), sg.FolderBrowse()],
    [sg.Text('Column 2', justification='center', size=(10, 1))],
    [sg.Column(col2)],
    [col5]]

window = sg.Window('Everything bagel', layout)

Upvotes: 1

Related Questions