here we will be learning a dropdown menu to select one out of many, like month or week day.
In this section we will be using OptionMenu.
below is the code to design a drop down menu to select day of the week.
#programtry:
from Tkinter import *
except:
from tkinter import *#Defining option list
OptionList = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
]
mw = Tk()
#Font and orientation setup
mw.geometry('400x60+50+50')
mw.title("AETies: Automation Expert Technologies")
var = StringVar(mw)
var.set(OptionList[0])
#Label
labelTest = Label(mw,text="select day", fg='red')
labelTest.pack(side="top")
opt = OptionMenu(mw, var, *OptionList)
opt.config(width=20, font=('Helvetica', 12))
opt.pack(side="top")
mainloop()
let's collect the value from OptionMenu
#program
try:
from Tkinter import *
except:
from tkinter import *
#Defining option list
OptionList = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
]
mw = Tk()
#Font and orientation setup
mw.geometry('400x60+50+50')
mw.title("AETies: Automation Expert Technologies")
variable = StringVar(mw)
variable.set(OptionList[0])
#Label
labelTest = Label(mw,text="select day", fg='red')
labelTest.pack(side="top")
opt = OptionMenu(mw, variable, *OptionList)
opt.config(width=20, font=('Helvetica', 12))
opt.pack(side="top")
#Function
def callback(*args):
labelTest.configure(text="The selected item is {}".format(variable.get()))
variable.trace("w", callback)
mainloop()
Comments
Post a Comment