简体   繁体   English

PySimpleGUI 表元素。 如何读取选定的行?

[英]PySimpleGUI Table element. How to read a selected row?

I am a beginner on Python and even more beginner on PySimpleGui, which I enjoy so much playing around in creating small apps with a GU interface.我是 Python 的初学者,甚至是 PySimpleGui 的初学者,我非常喜欢使用它来创建带有 GU 界面的小应用程序。 The issue I am having is about the table element.我遇到的问题是关于表格元素的。 So far, I managed to populate the table element with data from a sqlite database.到目前为止,我设法用 sqlite 数据库中的数据填充了表元素。 Once the table is ready with data, I can highlight a specific row.表格准备好数据后,我可以突出显示特定行。 As I am not aware of any event triggered by the table element, I placed a button to be clicked once a row has been selected and then execute the code associated to click event.由于我不知道由表格元素触发的任何事件,所以我放置了一个按钮,一旦选择了一行就可以单击它,然后执行与单击事件关联的代码。 Table ready, row selected, button clicked, and now, how to get the entire row values?准备好表,选择行,单击按钮,现在,如何获取整行值? I tried "values[' mytable ']" but the result were somehow like table index.我尝试了“values[' mytable ']”,但结果有点像表索引。 I would like to have a list of values.我想要一个值列表。 Is it possible?是否可以? Thanks for any help.谢谢你的帮助。

layout:布局:

data_values = []
data_headings = ['File ID', 'Type', 'Description', 'Remarks']
data_values.append(['', '', '', ''])
data_cols_width = [5, 8, 35, 35]
tab5_layout = [

[sg.Table(values=data_values, headings=data_headings,
                            max_col_width=65,
                            col_widths=data_cols_width,
                            auto_size_columns=False,
                            justification='left',
                            num_rows=6, key='_filestable_')],
    
[sg.Button('Select Row', key='_rowselected_')]

event:事件:

if event == '_rowselected_':
    te1 = values['_filestable_']
    print('Event triggered : ', te1)

When working with elements you've not used before in PySimpleGUI, it's helpful to look through several of the materials provided by the project.在 PySimpleGUI 中处理您以前未使用过的元素时,查看项目提供的一些材料会很有帮助。

One is the Demo Programs.一个是演示程序。 These are meant to give you a jump-start on using features.这些旨在让您快速开始使用功能。 The other is the call-reference documentation.另一个是调用参考文档。 You can access this same information via the docstrings (Control+Q when using PyCharm).您可以通过文档字符串访问相同的信息(使用 PyCharm 时按 Control+Q)。

I am not aware of any event triggered by the table element我不知道由表格元素触发的任何事件

If an element is capable of producing an event and doesn't do so by default (like a button or a menu), then you'll find a parameter enable_events .如果一个元素能够产生事件并且默认情况下不这样做(如按钮或菜单),那么您将找到一个参数enable_events

For the Table element, you'll find the docstring (and thus the call reference also) has a parameter for the table element:对于 Table 元素,您会发现文档字符串(以及调用参考)有一个用于 table 元素的参数:

enable_events – Turns on the element specific events. enable_events – 打开元素特定的事件。 Table events happen when row is clicked单击行时发生表事件

In your example, you need to do is add this parameter to your Table in the layout.在您的示例中,您需要做的是将此参数添加到布局中的表中。

[sg.Table(values=data_values, headings=data_headings,
                            max_col_width=65,
                            col_widths=data_cols_width,
                            auto_size_columns=False,
                            justification='left',
                            enable_events=True,
                            num_rows=6, key='_filestable_')],

EDIT:编辑:

I like to provide answers that teach how to solve the problems rather than just blurting out an answer.我喜欢提供教导如何解决问题的答案,而不是脱口而出的答案。 This will help you not just in this situation, but others as well when using the PySimpleGUI package.这不仅会在这种情况下对您有所帮助,在使用 PySimpleGUI package 时也会对您有所帮助。

Let's dig another step deeper though to understand how the values entry for Tables works.让我们再深入一步,了解表的值条目是如何工作的。

The entry in the values dictionary for Tables is a LIST of the rows selected.表的值字典中的条目是所选行的列表。

Be cautious if you see a construct such as this:如果您看到这样的构造,请小心:

data_values[values['_filestable_']]

as it won't run.因为它不会运行。 It will crash.它会崩溃。

The values cannot be used to directly lookup values in your source table.这些值不能用于直接在源表中查找值。 Trying to do so will generate an error that list indexes must be integers or slices.尝试这样做会产生一个错误,即列表索引必须是整数或切片。

Since events have been enabled in this example for the table, you can check for the event in your event loop and appropriately handle the event:由于在此示例中已为表启用了事件,因此您可以在事件循环中检查事件并适当地处理事件:

if event == '_filestable_':
    data_selected = [data_values[row] for row in values[event]]

data_selected will be a LIST of the rows of your original data. data_selected 将是原始数据行的列表。

If you wish to limit your user to selecting only 1 row at a time, then you can set the select_mode parameter when creating your table.如果您希望限制您的用户一次只能选择 1 行,那么您可以在创建表时设置select_mode参数。

select_mode=sg.TABLE_SELECT_MODE_BROWSE

Browse will allow single rows rather than the default of multiple rows.浏览将允许单行而不是默认的多行。 With the select mode of BROWSE set, you can assume your data_selected list will only have 1 entry, and thus data_selected[0] will be that entry.使用 BROWSE 设置的 select 模式,您可以假设您的data_selected列表只有 1 个条目,因此data_selected[0]将是该条目。 Or you can use data_selected[0] to get only the first row selected.或者您可以使用data_selected[0]只选择第一行。

One final word...最后一句话...

The PySimpleGUI documentation has a section on support that begins by explaining that sites such as SO are not recommended. PySimpleGUI 文档有一个关于支持的部分,首先解释了不推荐使用诸如 SO 之类的站点。 The recommendation is that these be avoided for numerous reasons, including a basic lack of knowledge.建议出于多种原因避免这些,包括基本缺乏知识。 For pretty much all GitHub projects that are active, filing an Issue with that project tends to provide high-quality answers.对于几乎所有活跃的 GitHub 项目,向该项目提交 Issue 往往会提供高质量的答案。 Most devs want to help and want to know of problems or common questions that perhaps mean the documentation isn't clear.大多数开发人员都想提供帮助并想知道可能意味着文档不清楚的问题或常见问题。

Good luck with your program.祝你的程序好运。 Hope it all works out well for you.希望这对你来说一切顺利。

Here you go:给你go:

This will give you the values of the selected row after you click the button.单击按钮后,这将为您提供所选行的值。

# --- EVENT LOOP ---
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == '_rowselected_':
        print(data_values[values['_filestable_']])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM