Python Forum
[Tkinter] populating dropdown from Method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] populating dropdown from Method
#3
(Apr-06-2020, 05:41 PM)deanhystad Wrote: It would be *self.options, not self.*options. But that has nothing to do with your problem.

I think your "*option" reference refers to this or a similar post on stackoverflow
choices = ('network one', 'network two', 'network three')

def refresh():
    # Reset var and delete all old options
    var.set('')
    network_select['menu'].delete(0, 'end')

    # Insert list of new options (tk._setit hooks them up to var)
    new_choices = ('one', 'two', 'three')
    for choice in new_choices:
        network_select['menu'].add_command(label=choice, command=tk._setit(var, choice))

network_select = tk.OptionMenu(root, var, *choices)
Here *choices unpacks the choices list into positional arguments so the OptionMenu command above becomes:
network_select = tk.OptionMenu(root, var, 'network one', 'network two', 'network three')
But that is not going to do what you want. That only sets an initial set of choices, I don't see that OptionMenu has any kind of variable that you can bind to automatically update the list. What you need to do is the other part of the post, the "refresh()". This method would bet called when you open a spreadsheet and get a list of the sheets. It clears out the old choices and builds a new list of choices.

My god, I spent 4 hours looking for this Big Grin
It was indeed as simple as adding the part below, I had never heard of tk._setit Dance
Thanks so much!

for choice in new_choices:
        network_select['menu'].add_command(label=choice, command=tk._setit(var, choice))
Reply


Messages In This Thread
populating dropdown from Method - by mikisDW - Apr-06-2020, 03:53 PM
RE: populating dropdown from Method - by deanhystad - Apr-06-2020, 05:41 PM
RE: populating dropdown from Method - by mikisDW - Apr-06-2020, 08:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Dropdown box showing weird entry cybertooth 4 2,179 Aug-16-2021, 03:45 PM
Last Post: deanhystad
  Option dropdown with Pyinquerer julio2000 0 1,513 Mar-22-2020, 04:11 PM
Last Post: julio2000
  [Tkinter] Choose from dropdown list and then do something? Selfiatus1 2 5,405 Jun-07-2019, 08:43 PM
Last Post: Selfiatus1
  PyQt, Open a Table when a row is selected populating it with the row values rarevesselt 18 15,165 Mar-30-2019, 12:57 AM
Last Post: rarevesselt
  Populating a Listbox from db Query DT2000 2 6,433 Feb-25-2019, 05:45 PM
Last Post: DT2000
  Dropdown menu- Store variable aking76 1 3,370 Sep-11-2018, 01:30 PM
Last Post: aking76

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020