python 中如何遍历listbox的每一个项目
在Python中,可以使用for循环来遍历listbox的每一个项目。下面是一个示例代码:
from tkinter import Tk, Listbox
# 创建一个窗口
window = Tk()
# 创建一个Listbox
listbox = Listbox(window)
listbox.pack()
# 向Listbox添加项目
listbox.insert(0, "项目1")
listbox.insert(1, "项目2")
listbox.insert(2, "项目3")
# 遍历Listbox的每一个项目
for item in listbox.get(0, "end"):
print(item)
# 运行窗口的主循环
window.mainloop()
在上面的示例代码中,首先创建了一个窗口和一个Listbox。然后使用insert方法向Listbox添加了三个项目。接下来使用for循环和get方法遍历了Listbox的每一个项目,并将项目打印出来。最后通过调用mainloop方法运行窗口的主循环。
原文地址: https://www.cveoy.top/t/topic/iHGd 著作权归作者所有。请勿转载和采集!