如何在Tkinter Treeview中取值并按国家分组执行While循环
解决方案:按国家分组遍历Treeview数据并执行操作
问题背景
我用Python创建了带有Treeview的GUI,支持添加国家、城市、道路条目,通过以下代码可获取每行数据:
for parent in info_tree.get_children(): print(info_tree.item(parent)["values"])
返回的数据示例:
["US", "New York", "5th Street"] ["US", "LA", "Main Street"] ["UK", "London", "Waterloo Road"]
需求:通过while循环遍历这些数据,当国家列值相同时(如US),对该国家对应的所有行的城市和道路执行操作,直到出现不同国家。设想的伪代码如下:
While current country == previous country: cty = driver.find_element(By.NAME, "city") cty.click() rd = driver.find_element(By.Name, "road") rd.click()
修改后的完整代码
以下是实现该需求的完整代码,核心是在run_task函数中按国家分组处理数据:
import tkinter from tkinter import * from tkinter import ttk window = Tk() # 窗口配置 window.title("Report Generator") window.configure(bg='blue') window.resizable(0,0) # 核心任务函数:按国家分组处理数据 def run_task(): # 先提取所有Treeview数据到列表 all_data = [] for parent in info_tree.get_children(): all_data.append(info_tree.item(parent)["values"]) if not all_data: return index = 0 total_rows = len(all_data) while index < total_rows: current_country = all_data[index][0] # 处理当前国家的所有条目 print(f"开始处理国家: {current_country}") while index < total_rows and all_data[index][0] == current_country: country, city, road = all_data[index] # 这里替换成你实际的操作,比如Selenium的点击逻辑 print(f"处理城市: {city}, 道路: {road}") # 示例操作(替换为你的driver代码) # cty = driver.find_element(By.NAME, "city") # cty.click() # rd = driver.find_element(By.NAME, "road") # rd.click() index += 1 print(f"完成国家 {current_country} 的所有处理\n") frame = tkinter.Frame(bg='blue') # Treeview配置 info_tree = ttk.Treeview(frame, columns=(1,2,3), show="headings", height="6") info_tree["columns"] = ("Country", "City", "Road") count=0 data =[] for record in data: info_tree.insert(parent="", index="end", iid=count,values=(record[0], record[1], record[2] )) count+= 1 info_tree.column("#0", width=0, stretch=NO) info_tree.column("#1", width=120, anchor=W) info_tree.column("#2", width=120, anchor=W) info_tree.column("#3", width=120, anchor=W) info_tree.heading("#0", text="") info_tree.heading("#1", text="Country", anchor=CENTER) info_tree.heading("#2", text="City", anchor=CENTER) info_tree.heading("#3", text="Road", anchor=CENTER) # 插入测试数据 data = [ ["US", "New York", "5th Street"], ["US", "LA", "Main Street"], ["UK", "London", "Waterloo Road"] ] for record in data: info_tree.insert("", index="end", iid=count,values=(record[0], record[1], record[2])) count +=1 # 创建控件 country_add = tkinter.Label(frame, text="Country:", bg='blue', fg="#FFFFFF", font=('Arial 12')) city_add = tkinter.Label(frame, text="City:", bg='blue', fg="#FFFFFF", font=('Arial 12')) road_add = tkinter.Label(frame, text="Road Name:", bg='blue', fg="#FFFFFF", font=('Arial 12')) country_entry = tkinter.Entry(frame, font=("Arial", 10), justify=LEFT) city_entry = tkinter.Entry(frame, font=("Arial", 10), justify=LEFT) road_entry = tkinter.Entry(frame, font=("Arial", 10), justify=LEFT) submit_button = tkinter.Button(frame, text="Submit", fg="#000000", font=('Arial 12'), command=run_task) # 布局控件 info_tree.grid(row=0, column=0, columnspan=3, pady=5, padx=20, sticky=EW) country_add.grid(row=1, column=0, pady=[10,0]) country_entry.grid(row=2, column=0, pady=[0,5], padx=[10,0], sticky=W) city_add.grid(row=1, column=1, pady=[10,0]) city_entry.grid(row=2, column=1, pady=[0,5], padx=[10,0], sticky=W) road_add.grid(row=1, column=2, pady=[10,0]) road_entry.grid(row=2, column=2, pady=[0,5], padx=[10,0], sticky=W) submit_button.grid(row=14, column=0, columnspan=2, pady=20) frame.pack() window.mainloop()
关键逻辑说明
- 提取数据:先把Treeview中的所有数据一次性提取到列表
all_data,方便后续遍历操作。 - 双层while循环:
- 外层循环控制当前处理的国家分组,每次切换到新国家时更新
current_country。 - 内层循环处理当前国家的所有条目,直到遇到不同国家或遍历完所有数据。
- 外层循环控制当前处理的国家分组,每次切换到新国家时更新
- 操作替换:把示例中的
print语句替换为你实际的Selenium操作(比如查找元素、点击等)即可。
内容的提问来源于stack exchange,提问作者rev7777
相关产品推荐
相关产品推荐

