[英]How to obtain the row and column of selected cell in PySimpleGUI?
I have some tabular data, that I want displayed in my GUI.我有一些表格数据,我想在我的 GUI 中显示它们。 I am using PySimpleGUI.我正在使用 PySimpleGUI。
I am building on this answer .我正在建立这个答案。
[sg.Table(values=data_values, headings=data_headings,
max_col_width=65,
col_widths=data_cols_width,
auto_size_columns=False,
select_mode=sg.TABLE_SELECT_MODE_BROWSE,
justification='left',
enable_events=True,
num_rows=6, key='_tracker_')]
The table has enabled events, hence every click on it creates a event with the key value.该表启用了事件,因此每次点击它都会创建一个带有键值的事件。
while True:
event, values = window.read()
if event == "_tracker_":
print(values["_tracker_"])
I can obtain the row of the click with values["_tracker_"]
.我可以使用values["_tracker_"]
获取点击行。 Can I get the column as well?我也可以得到专栏吗?
Basically I want the event to be triggered only when the table is clicked in a specific column, while the row of the event is variable.基本上我希望只有在特定列中单击表时才触发事件,而事件的行是可变的。 Hence, I thought the best way was to check events on whole table and than filter out based on row and column.因此,我认为最好的方法是检查整个表上的事件,而不是根据行和列过滤掉。
If there is a completely other way to do this, I am open to suggestions.如果有完全其他的方法可以做到这一点,我愿意接受建议。
It can work by tkinter code.它可以通过 tkinter 代码工作。
event
stored in element.user_bind_event
event
存储在element.user_bind_event
identify('region', x, y)
of widget of element.查找通过元素小部件的方法identify('region', x, y)
单击identify('region', x, y)
。identify_row(y)
of widget of element.通过元素小部件的方法identify_row(y)
确定哪一行。identify_column(x)
of widget of element.通过元素小部件的方法identify_column(x)
确定哪一列。Here's my code for it.这是我的代码。
import PySimpleGUI as sg
sg.theme("DarkBlue3")
newlist = [[f"Cell ({row+1}, {col+1})" for col in range(8)] for row in range(10)]
COL_HEADINGS = ["Date", "Ref", "ID", "Owner", "Customer", "Price", "Size", "Path"]
layout = [
[sg.Table(values=newlist, headings=COL_HEADINGS, max_col_width=25,
num_rows=10, alternating_row_color='green', key='-TABLE-',
enable_events=True, justification='center',)],
[sg.Text("", size=(50, 1), key='-Position-')]
]
window = sg.Window('Table', layout, finalize=True)
table = window['-TABLE-']
table.bind('<Button-1>', "Click")
position = window['-Position-']
position.expand(expand_x=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == '-TABLE-':
pass
elif event == '-TABLE-Click':
e = table.user_bind_event
region = table.Widget.identify('region', e.x, e.y)
if region == 'heading':
row = 0
elif region == 'cell':
row = int(table.Widget.identify_row(e.y))
elif region == 'separator':
continue
else:
continue
column = int(table.Widget.identify_column(e.x)[1:])
position.update(f"Table clicked at ({row}, {column})")
window.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.