Skip to content
Advertisement

Is it possible to insert a checkbox in a PysimpleGui table?

I want to create a PySimpleGui table where multiple rows can be selected just using the mouse (for an app in python3). The pysimplegui table allows to select various rows by means of Ctrl and Shift as usual, but i need to do this only by clicking at the rows.

I have tried to do it by inserting a checkbox in the list of values loaded by the table but, as expected, i got the TypeError: 'Checkbox' object is not iterable error.

Is there anyway to do this in PySimpleGui, even without any checkbox? Thanks in advance for any idea

Advertisement

Answer

enter image description here

Here’s my way to go by using sg.Tree

from io import BytesIO
from PIL import Image, ImageDraw
import PySimpleGUI as sg

def icon(check):
    box = (32, 32)
    background = (255, 255, 255, 0)
    rectangle = (3, 3, 29, 29)
    line = ((9, 17), (15, 23), (23, 9))
    im = Image.new('RGBA', box, background)
    draw = ImageDraw.Draw(im, 'RGBA')
    draw.rectangle(rectangle, outline='black', width=3)
    if check == 1:
        draw.line(line, fill='black', width=3, joint='curve')
    elif check == 2:
        draw.line(line, fill='grey', width=3, joint='curve')
    with BytesIO() as output:
        im.save(output, format="PNG")
        png = output.getvalue()
    return png

check = [icon(0), icon(1), icon(2)]

headings = ['President', 'Date of Birth', '1', '2', '3']
data = [
    ['Ronald Reagan', 'February 6'],
    ['Abraham Lincoln', 'February 12'],
    ['George Washington', 'February 22'],
    ['Andrew Jackson', 'March 15'],
    ['Thomas Jefferson', 'April 13'],
    ['Harry Truman', 'May 8'],
    ['John F. Kennedy', 'May 29'],
    ['George H. W. Bush', 'June 12'],
    ['George W. Bush', 'July 6'],
    ['John Quincy Adams', 'July 11'],
    ['Garrett Walker', 'July 18'],
    ['Bill Clinton', 'August 19'],
    ['Jimmy Carter', 'October 1'],
    ['John Adams', 'October 30'],
    ['Theodore Roosevelt', 'October 27'],
    ['Frank Underwood', 'November 5'],
    ['Woodrow Wilson', 'December 28'],
]

treedata = sg.TreeData()
for president, birthday in data:
    treedata.Insert('', president, president, values=[birthday]+[1,2,3],
    icon=check[0])

sg.theme('LightPurple')
sg.set_options(font=('Helvetica', 16))
layout = [
    [sg.Tree(data=treedata, headings=headings[1:], auto_size_columns=True,
        num_rows=10, col0_width=20, key='-TREE-', row_height=48, metadata=[],
        show_expanded=False, enable_events=True,
        select_mode=sg.TABLE_SELECT_MODE_BROWSE)],
    [sg.Button('Quit')]
]
window = sg.Window('Tree as Table', layout, finalize=True)
tree = window['-TREE-']
tree.Widget.heading("#0", text=headings[0]) # Set heading for column #0

while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Quit'):
        break
    elif event == '-TREE-':
        president = values['-TREE-'][0]
        print(president)
        if president in tree.metadata:
            tree.metadata.remove(president)
            tree.update(key=president, icon=check[0])
        else:
            tree.metadata.append(president)
            tree.update(key=president, icon=check[1])

window.close()
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement