微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

迭代 Treeview 项并获取值

如何解决迭代 Treeview 项并获取值

我有以下 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',width=20)
listBox.column('#3',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 的网站上填写某些表单。

enter image description here

示例:

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') 没有给我我需要的结果。

解决方法

您可以使用 Treeview 获取 .get_children() 的所有 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。