我正在尝试在Tkinter(下拉菜单)中实现OptionMenu
目前它只显示文本文件中的第一行,它应该根据文本文件中的行数在OptionMenu中显示一个新选项。相反,它只是在一个完整的选项中显示它们
Canvas(height = 600, width = 800).place(x=0,y=0)
with open('football.txt') as txt:
footballFixtures = [cut.strip().split(':') for cut in txt.readlines()]
for typeoffixture,descriptionoffixture,dateoffixture,resultoffixture in footballFixtures:
desc = [descriptionoffixture]
first = StringVar()
first.set("Select a Fixture")
OptionMenu(root,first, *desc).place(x=90, y=150)
文本文件的复制
Type of Fixture - Football:Description of Fixture - Test vs Test:Date of Fixture - 10-12-1998:Result of Fixture - To-be-Announced
Type of Fixture - Football:Description of Fixture - Test vs Testt:Date of Fixture - 10-12-2013:Result of Fixture - To-be-Announced
答案 0 :(得分:2)
如果我理解得很好,你的代码应该是这样的:
desc = []
with open('football.txt') as txt:
footballFixtures = [cut.strip().split(':') for cut in txt.readlines()]
print footballFixtures
for typeoffixture,descriptionoffixture,dateoffixture,resultoffixture in footballFixtures:
desc.append(descriptionoffixture)
first = StringVar()
first.set("Select a Fixture")
OptionMenu(root,first, *desc).place(x=90, y=150)
答案 1 :(得分:1)
您正在为文件中的每一行创建一个OptionMenu,并且您将它们堆叠在一起。将OptionMenu的创建移到循环之外。