pysimplegui库中的table控件的用法
时间: 2024-09-26 15:08:46 浏览: 40
PySimpleGUI的Table控件允许你创建表格形式的用户界面元素,用于展示数据。以下是使用`Table`的基本步骤[^1]:
1. **导入库**:
```python
import PySimpleGUI as sg
```
2. **定义表头**:
```python
headings = ['Column 1', 'Column 2', 'Column 3']
```
3. **初始化表单**:
```python
layout = [[sg.Table(values=[], headings=headings, num_rows=10, key='-TABLE-')]]
```
4. **创建窗口**:
```python
window = sg.Window('Table Example', layout)
```
5. **填充数据** (你可以通过`update_table`函数添加或更新数据):
```python
table_data = [['Row 1 Value 1', 'Row 1 Value 2', 'Row 1 Value 3'],
['Row 2 Value 1', 'Row 2 Value 2', 'Row 2 Value 3']]
window['-TABLE-'].update(table_data)
```
6. **事件循环**:
```python
event, values = window.read()
```
7. **关闭窗口**:
```python
if event == sg.WINDOW_CLOSED or event == 'Cancel':
window.close()
```
完整示例:
```python
import PySimpleGUI as sg
headings = ['Column 1', 'Column 2', 'Column 3']
layout = [[sg.Table(values=[], headings=headings, num_rows=10, key='-TABLE-')]]
window = sg.Window('Table Example', layout)
table_data = [['Row 1 Value 1', 'Row 1 Value 2', 'Row 1 Value 3'],
['Row 2 Value 1', 'Row 2 Value 2', 'Row 2 Value 3']]
window['-TABLE-'].update(table_data)
event, values = window.read()
if event == sg.WINDOW_CLOSED or event == 'Cancel':
window.close()
```
阅读全文