如何在PySimpleGUI的Table组件中动态修改列可见性
PySimpleGUI Table列可见性切换实现方案
不需要创建两个独立的Table组件,直接通过Table组件的update方法修改visible_column_map参数即可实现列可见性翻转,原有代码更新失败是两个问题导致的:
- 调用
update的控件对象错误:visible_column_map是Table控件的专属属性,原有代码错误地给sg.Text类型的Birthday控件传递了该参数 - 语法存在疏漏:
update调用末尾缺少右闭合括号
可运行的完整示例代码
import PySimpleGUI as sg headings = ['总统姓名', '出生日期'] data = [ ['罗纳德·里根', '2月6日'], ['亚伯拉罕·林肯', '2月12日'], ['乔治·华盛顿', '2月22日'], ['安德鲁·杰克逊', '3月15日'], ['托马斯·杰斐逊', '4月13日'], ] sg.theme('DarkBlue3') sg.set_options(font=("Courier New", 12)) layout = [ [sg.Table(data, headings=headings, visible_column_map=[True, False], justification='left', select_mode=sg.TABLE_SELECT_MODE_BROWSE, enable_events=True, key='President')], [sg.Button('切换列可见性', key='toggle')], [sg.Text('', size=(30, 1), key='Birthday')], ] window = sg.Window("列切换演示", layout, finalize=True) # 记录当前列可见状态 current_visible = [True, False] while True: event, values = window.read() if event == sg.WINDOW_CLOSED: break elif event == 'President': index = values[event][0] window['Birthday'].update(f"选中人物出生日期:{data[index][1]}") elif event == 'toggle': # 翻转两列的可见性 current_visible = [not status for status in current_visible] # 给Table控件调用update更新可见列配置 window['President'].update(visible_column_map=current_visible) window.close()
注意事项
如果运行时提示visible_column_map参数不存在,请升级你的PySimpleGUI到最新版本,升级命令为:pip install --upgrade PySimpleGUI
内容的提问来源于stack exchange,提问作者cryptoref
相关产品推荐
相关产品推荐

