6

I'm creating a GUI using Tkinter with Python 2.7.6.

I have a drop down menu, created and initially disabled with the following code:

    self.dropdown = Tkinter.OptionMenu(self, self.dropdownVar, "Select SED...")
    self.dropdown.grid(column=0,row=1)
    self.dropdown.configure(state="disabled")

After a user selects a directory, I call a function onEnterDir() which then gets a list of files in that directory. So, I have a list of files in a variable called dirFiles.

What I want is to then update the options in the dropdown menu with the items in this dirFiles list. How would I do this?

My question is different to others on here because I just want to update the list of items self.dropdown displays. It's not dependent on any other widget. I have a python list of things I want to put in. How do I do this?

falsetru
  • 357,413
  • 63
  • 732
  • 636
Carl M
  • 215
  • 3
  • 10

3 Answers3

4

You can use the same technique in the answer you mentioned in question:

For example:

import os
from functools import partial
from Tkinter import *
from tkFileDialog import askdirectory

def onEnterDir(dropdown, var):
    path = askdirectory()
    if not path:
        return
    filenames = os.listdir(path)
    dropdown.configure(state='normal')  # Enable drop down
    menu = dropdown['menu']

    # Clear the menu.
    menu.delete(0, 'end')
    for name in filenames:
        # Add menu items.
        menu.add_command(label=name, command=lambda name=name: var.set(name))
        # OR menu.add_command(label=name, command=partial(var.set, name))


root = Tk()
dropdownVar = StringVar()
dropdown = OptionMenu(root, dropdownVar, "Select SED...")
dropdown.grid(column=0, row=1)
dropdown.configure(state="disabled")
b = Button(root, text='Change directory',
           command=lambda: onEnterDir(dropdown, dropdownVar))
b.grid(column=1, row=1)
root.mainloop()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    why in the GUI, created by this code, just that the last item can be selected? – rgholizadehr May 03 '19 at 18:25
  • @R.GholizadehR., I don't get it. – falsetru May 04 '19 at 08:42
  • 1
    @ falsetru, actually, when a directory is selected, its content is shown in the option menu but selection does not work. e.g. there are 10 items in the option menu; if you select the second item, the option menu shows the 10th item and it is locked there. – rgholizadehr May 04 '19 at 11:06
  • @R.GholizadehR., Ah, there was a bug; Replace `menu.add_command` line. I just updated the command accordingly; before the update, `name` in the lambda referenced the last `name` value (cause). – falsetru May 04 '19 at 13:36
0

Have to set self.dropdown to an active state first, and then put in the list of options like this:

    self.dropdown.configure(state="active")
    for fi in dirList:
        self.dropdown['menu'].add_command(label=fi, command=Tkinter._setit(self, fi))

The command bit was what was throwing me off.

Carl M
  • 215
  • 3
  • 10
0

You have OptionMenu.set_menu(*values), which does the similar things in @falsetru 's answer.

kakyo
  • 10,460
  • 14
  • 76
  • 140