问题描述
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 的网站上填写某些表单。
示例:
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)