Tkinter OptionMenu Create default option
So far, the program has just been executed without seeing any items, but we can use the set( ) method to create default options for this OptionMenu.
Example#1
Use the set() method to establish the default options.
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
omTuple = ("Python","Java","C") # Use Tuple
# omTuple = ["Python","Java","C"] # Use List
var = StringVar(root)
var.set("Python")
optionmenu = OptionMenu(root,var,*omTuple)
optionmenu.pack()
root.mainloop()
Output:
The above program successfully sets the default values, but that is not a good design. It is recommended that since you have used tuples to build list items, you can use the tuple variable name + index method to set the default options.
Example#2
Set the default option using the tuple variable name + index method.
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
omTuple = ("Python","Java","C") # Use Tuple
# omTuple = ["Python","Java","C"] # Use List
var = StringVar(root)
var.set(omTuple[0])
optionmenu = OptionMenu(root,var,*omTuple)
optionmenu.pack()
root.mainloop()
Output: