I am a beginner in python, normally if i want to add option menu to my program, i would do something like this.
from Tkinter import*
root=Tk()
mylist=['a','b','c']
var=StringVar(root)
var.set("Select status")
mymenu=OptionMenu(root,var,*mylist)
mymenu.pack()
mymenu.config(font=('calibri',(10)),bg='white',width=12)
mymenu['menu'].config(font=('calibri',(10)),bg='white')
root.mainloop()
It works fine but i am wondering if there is any shorter way to achieve the same result as each option menu will take 7 lines of code. I have to create several option menu so i am looking for a proper and shorter way to do it.
EDIT: Someone pointed out to create function that will generate option menu. So i tried this,
from Tkinter import*
def Dropmenu(mylist,status):
var=StringVar(root)
var.set(status)
mymenu=OptionMenu(root,var,*mylist)
mymenu.pack(side=LEFT)
mymenu.config(font=('calibri',(10)),bg='white',width=12)
mymenu['menu'].config(font=('calibri',(10)),bg='white')
root=Tk()
Dropmenu(['a','b','c'],'Select')
root.mainloop()
But now, how do i address the "var" so that i can fetch all the values that user chose? According to my example, all the option menu will have the same "var" value, so i have no way to get the choices that user made for different option menu.
To make things more clear,let's say if i have 2 option menu
Dropmenu(['a','b','c'],'Select')
Dropmenu(['c','d','e'],'Select')
If i use
myvalue=var.get()
Since both option menus have the same var name, how do i fetch the both values?