How can I create a dropdown menu from a List in Tkinter?

Viewed 221843

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?

2 Answers

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()
Related