python - 是否可以在 PysimpleGui 表中插入复选框?

标签 python python-3.x user-interface pysimplegui

我想创建一个 PySimpleGui 表,其中可以仅使用鼠标选择多行(对于 python3 中的应用程序)。 pysimplegui 表允许像往常一样通过 Ctrl 和 Shift 选择各种行,但我只需要通过单击行来执行此操作。

我试图通过在表加载的值列表中插入一个复选框来做到这一点,但正如预期的那样,我得到了 TypeError: 'Checkbox' object is not iterable错误。

是否有在 PySimpleGui 中执行此操作,即使没有任何复选框? 提前感谢您的任何想法

最佳答案

enter image description here

这是我使用 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()

关于python - 是否可以在 PysimpleGui 表中插入复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65773214/

相关文章:

android - 适用于 Android 的 PyQt5 GUI 与 Kivy GUI

python - 我无法在原子文本编辑器中安装autocomplete-python软件包

python - 去掉输出中的括号

python - python3中具有不同签名的多重继承

Java Swing : Separation of logic and GUI

open-source - 推荐开源项目,UI开发

python - 如何根据模型与特定对象的关联为模型添加辅助 ID?

python - 桶排序比快速排序更快

python-3.x - pandas pd.read_table 支持 io.BytesIO 和 StringIO 吗?

java - 更改后我应该重画GUI吗?