سؤال

I'm trying to implement an OptionMenu in Tkinter (Dropdown menu)

At the moment it only displays the first line in the text file and it should display a new option in the OptionMenu based on the amount of lines in the text file. Instead it just displays them in one whole option

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)

Copy of Text File

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
هل كانت مفيدة؟

المحلول

If i understood well, you code should look like this:

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)

نصائح أخرى

You are creating one OptionMenu for each line in the file, and you are stacking them on top of each other. Move the creation of the OptionMenu outside of your loop.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top