【问题标题】:Iterate over Treeview items and get value迭代 Treeview 项目并获得价值
【发布时间】:2021-09-25 03:38:38
【问题描述】:

我有一个 ttk Treeview 的以下代码:

listbox = ttk.Treeview(
    tab_player,
    columns=('Player', 'Rating', 'Price'),
    selectmode="extended",
    show="headings"
)

listbox.heading('#1', text='Player', anchor=tk.CENTER)
listbox.heading('#2', text='Rating', anchor=tk.CENTER)
listbox.heading('#3', text='Price', anchor=tk.CENTER)
listbox.column('#1', stretch=tk.YES, minwidth=50, width=80)
listbox.column('#2', stretch=tk.YES, minwidth=50, width=20)
listbox.column('#3', stretch=tk.YES, minwidth=50, width=30)

listbox.grid(row=5, column=5, rowspan=7, sticky=W)

我的插入函数如下:

def insertitem():
        GUI.listbox.insert('', 'end', values = (GUI.listbox_content.get(), 
                                                GUI.listboxr_content.get(), 
                                                GUI.listbox_content_price.get()))

稍后我想遍历 Treeview 并从每行的每一列中获取值,以使用这些值在带有 selenium 的网站上填写某些表单。

示例:

str_libo_p = GUI.listbox.column.__getattribute__('Player')
str_libo_r = GUI.listbox.column.__getattribute__('Rating')
str_libo_price = GUI.listbox.column.__getattribute__('Price')

                #### some uninteresting code in between

                for i, r, p in zip(str_libo_p, str_libo_r, str_libo_price):
                    # values will be used to fill specific entry fields on a website.
                    # just as an example to let you know what I am using it for:
                    text_playername = init_webdriver.wait.until(
                        EC.element_to_be_clickable(
                            (
                                By.XPATH,
                                (
                                    #####
                                ),
                            )
                        )
                    )
                    text_playername.click()
                    text_playername.send_keys(i)

                    # same will be done for text_player_rating.send_keys(r) and 
                    # text_player_price.send_keys(p) with different XPATHs
                    

从文档中我不明白我应该在这里调用哪个函数来获得一个值作为回报。 GUI.listbox.column.__getattribute__('Price') 没有给我我需要的结果。

【问题讨论】:

    标签: python tkinter widget ttk


    【解决方案1】:

    您可以使用.get_children() 获取Treeview 的所有row_id,并使用.item(row_id)["value"] 获取每一行的值:

    for row_id in GUI.listbox.get_children():
        i, r, p = GUI.listbox.item(row_id)["values"]
        ...
        text_playername.send_keys(i)
    

    【讨论】:

    • 谢谢。但是我必须以某种方式从每行的每一列中获取值作为自己的值。如果我说 i,r,p = GUI.listbox.item(row_id)["value"] 它将一行中的所有三列作为一个值,或者?
    • i 是玩家姓名,r 是等级,p 是价格。
    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多