user8371286
user8371286

Reputation:

Drop down menu changes order and removes one choice each time - Python

Python drop down menu changes order each time it is ran and removes one choice each time. How do you fix this.

from tkinter import *
from tkinter.ttk import *

root = Tk()
root.title("Menu")

menu = Frame(root)                         

menu.pack(pady = 5, padx = 50)

var = StringVar(root)

list = {
    '1',
    '2',
    '3',
    '4',
}

option = OptionMenu(menu, var, * list)
var.set('Select')

option.grid(row = 1, column = 1)

root.mainloop()

Upvotes: 2

Views: 569

Answers (1)

zwer
zwer

Reputation: 25789

Do not use a set (an unordered structure) to define your options, use a list, e.g.:

options = [  # notice the square bracket
    '1',
    '2',
    '3',
    '4'
]
option = OptionMenu(menu, var, options[0], *options)  # make sure you define a proper default
# etc.

As for the removal of the first element - it was happening because you didn't define the third argument to OptionMenu which sets the default value so your first options element was expanded into it.

P.S. It's a very, very bad idea to name your variables/functions the same name as built-in types (e.g. list in your case).

Upvotes: 3

Related Questions