Search code examples
pythontkinterdropdown

displaying lists vertically tkinter and python


I am learning tkinter with python 3.7 and trying to understand dropdown lists. I am trying to get the list to display vertically, so when I select and item with the button it appears underneath the button. But the list keeps displaying horizontal so I cant select one item at a time - any help please.

from tkinter import *

root = Tk()
# set in pixels
root.geometry("400x400")


def selected():
    my_label = Label(root, text=clicked.get()).pack()


options = [
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
]

clicked = StringVar()
clicked.set(options[0])

drop = OptionMenu(root, clicked, options)
drop.pack(pady=100)

myButton = Button(root, text="selected from list", command=selected)
myButton.pack()

root.mainloop()

Solution

  • The option menu requires distict options. It sees the entire list as a single option.

    Us python's * operator to expand yout list:

    drop = OptionMenu(root, clicked, *options)