55

I am creating a GUI that builds information about a person. I want the user to select their birth month using a drop down bar, with the months configured earlier as a list format.

from tkinter import *

birth_month = [
    'Jan',
    'Feb',
    'March',
    'April'
    ]   #etc


def click():
    entered_text = entry.get()

Data = Tk()
Data.title('Data') #Title

label = Label(Data, text='Birth month select:')
label.grid(row=2, column=0, sticky=W) #Select title

How can I create a drop down list to display the months?

Ethan Field
  • 4,646
  • 3
  • 22
  • 43

2 Answers2

107

To create a "drop down menu" you can use OptionMenu in tkinter

Example of a basic OptionMenu:

from Tkinter import *

master = Tk()

variable = StringVar(master)
variable.set("one") # default value

w = OptionMenu(master, variable, "one", "two", "three")
w.pack()

mainloop()

More information (including the script above) can be found here.


Creating an OptionMenu of the months from a list would be as simple as:

from tkinter import *

OPTIONS = [
"Jan",
"Feb",
"Mar"
] #etc

master = Tk()

variable = StringVar(master)
variable.set(OPTIONS[0]) # default value

w = OptionMenu(master, variable, *OPTIONS)
w.pack()

mainloop()

In order to retrieve the value the user has selected you can simply use a .get() on the variable that we assigned to the widget, in the below case this is variable:

from tkinter import *

OPTIONS = [
"Jan",
"Feb",
"Mar"
] #etc

master = Tk()

variable = StringVar(master)
variable.set(OPTIONS[0]) # default value

w = OptionMenu(master, variable, *OPTIONS)
w.pack()

def ok():
    print ("value is:" + variable.get())

button = Button(master, text="OK", command=ok)
button.pack()

mainloop()

I would highly recommend reading through this site for further basic tkinter information as the above examples are modified from that site.

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
Ethan Field
  • 4,646
  • 3
  • 22
  • 43
  • "need to use" is a bit strong. Another choice is a combobox provided by the `ttk` module. – Bryan Oakley Aug 01 '17 at 16:12
  • 1
    Updated to reflect – Ethan Field Aug 01 '17 at 20:30
  • 1
    The [effbot.org](https://effbot.org/) you have linked to is now inactive ("in hiatus"). Please consider updating the answer with some alternative (or remove the reference altogether). – desertnaut Dec 20 '20 at 16:30
  • 2
    For the first link: http://web.archive.org/web/20201113091416/https://effbot.org/tkinterbook/optionmenu.htm and for the second one: http://web.archive.org/web/20201111171246/https://effbot.org/tkinterbook/ – TheLizzard Jan 22 '21 at 00:01
2

Here Is my function which will let you create a Combo Box with values of files stored in a Directory and Prints the Selected Option value in a Button Click.

from tkinter import*
import os, fnmatch

def submitForm():    
    strFile = optVariable.get()
    # Print the selected value from Option (Combo Box)    
    if (strFile !=''):        
        print('Selected Value is : ' + strFile)


root = Tk()
root.geometry('500x500')
root.title("Demo Form ")


label_2 = Label(root, text="Choose Files ",width=20,font=("bold", 10))
label_2.place(x=68,y=250)

flist = fnmatch.filter(os.listdir('.'), '*.mp4')
optVariable = StringVar(root)
optVariable.set("   Select   ") # default value
optFiles = OptionMenu(root, optVariable,*flist)
optFiles.pack()
optFiles.place(x=240,y=250)

Button(root, text='Submit', command=submitForm, width=20,bg='brown',fg='white').place(x=180,y=380)


root.mainloop()
Chikku Jacob
  • 2,114
  • 1
  • 18
  • 33
  • 5
    This script is nothing like the example code that OP gave and contains information related to reading directories which has nothing to do with the question. On a question like this which is asking for a very simple, very generic answer I'd advise not having as much unrelated bloat. – Ethan Field May 28 '21 at 11:49