-1

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()
Sid
  • 153
  • 3
  • 15
  • You should use a combobox: https://www.geeksforgeeks.org/combobox-widget-in-tkinter-python/ – Jean-Marc Volle Apr 15 '20 at 13:51
  • ***the list keeps displaying horizontal***: Can't reproduce this, [edit] your question and add a image to show this behaviour. – stovfl Apr 15 '20 at 14:20
  • @stovfl: I can reproduce this. Are you _certain_ you can't? I can't see how this code could work anyway other than the way described in the question. – Bryan Oakley Apr 15 '20 at 14:28
  • Does this answer your question? [how-do-populate-a-tkinter-optionmenu-with-items-in-a-list](https://stackoverflow.com/questions/18212645) – stovfl Apr 15 '20 at 15:54

1 Answers1

1

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)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for that - is there any documentation on the *options and how its used- just had a look but cant find anything – Sid Apr 15 '20 at 15:09
  • @Sid: the behavior of `*` is documented in the standard python documentation: https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists – Bryan Oakley Apr 15 '20 at 15:53