英文:
How to get last item of a specific column in tkinter python?
问题
要获取特定列的最后一项数值,您可以使用以下代码:
```python
for child in OUT_DC_Level_data_table.get_children():
item_values = OUT_DC_Level_data_table.item(child)['values']
get_latest_value_OUT_DC_level_frozen = int(item_values[1][-1])
get_latest_value_OUT_DC_level_chill = int(item_values[2][-1])
get_latest_value_OUT_DC_level_dry = int(item_values[3][-1])
<details>
<summary>英文:</summary>
So I want to get the last item value of a specific column. How do you do this? All I know is to get all value of tkinter treeview. I have tried everything, but I am still clueless. The code I did below get error *TypeError: 'int' object is not subscriptable*. I want the data of the get_latest_value to be able to make calculation, such as get_latest_value_frozen * 30
for child in OUT_DC_Level_data_table.get_children():
item_values = OUT_DC_Level_data_table.item(child)['values']
get_latest_value_OUT_DC_level_frozen = int(OUT_DC_Level_data_table.item(child)['values'][1][-1])
get_latest_value_OUT_DC_level_chill = int(OUT_DC_Level_data_table.item(child)['values'][1][-1])
get_latest_value_OUT_DC_level_dry = int(OUT_DC_Level_data_table.item(child)['values'][1][-1])
</details>
# 答案1
**得分**: 1
首先,您获取最后一项的 *项目 ID*,然后使用 `.set(item_id, column_name)` 获取所需的值:
```python
# 获取最后一项的项目 ID
item_id = OUT_DC_Level_data_table.get_children()[-1]
# 例如,获取列 "frozen" 的最新值
get_latest_value_OUT_DC_level_frozen = int(OUT_DC_Level_data_table.set(item_id, "frozen"))
# 类似地获取其他列的值
英文:
First you get the item ID of the last item, then use .set(item_id, column_name)
to get the required values:
# get the item ID of the last item
item_id = OUT_DC_Level_data_table.get_children()[-1]
# get the value of column "frozen" for example
get_latest_value_OUT_DC_level_frozen = int(OUT_DC_Level_data_table.set(item_id, "frozen"))
# get values of other columns similarly
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论