« Basics of Python Tkinter « OptionMenu

Components in our GUI window
We will have one text box , one Button and one OptionMenu. User can enter data in the textbox and on click of Add button the entered data will be added as option in our OptionMenu.
Component ( Text ) | Function | Action performed by the Function |
l1 ( Enter Option ) | - | Display Message only before the OptionMenu |
t1 | - | User enter text for adding as option of the OptionMenu |
b1 (Add) | my_add() | Read the entered text ( t1 ) and add as option to OptionMenu |
om1 | -- | The OptionMenu to which the entered text will be added as Option |
l2 ( Output ) | -- | Display messages if Option is added or not added ( already available ) |
Here is the code to add the component to our GUI window.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_list = ["PHP","MySQL","Python","HTML","JQuery"]
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value
l1 = tk.Label(my_w, text='Enter Option', width=10 ) # added one Label
l1.grid(row=2,column=1)
t1 = tk.Text(my_w, height=1, width=8,bg='yellow') # added one text box
t1.grid(row=2,column=2)
b1 = tk.Button(my_w, text='Add', command=lambda: my_add() )
b1.grid(row=2,column=4)
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5)
my_str = tk.StringVar()
l2 = tk.Label(my_w, text='Output',textvariable=my_str, width=20 ) # added one Label
l2.grid(row=3,column=1)
In above code we have used on click event of our Button b1 to execute the function my_add()
my_add()
This function is triggered when Button b1 is clicked. First we will create one list by using the available options in our OptionMenu om1. We used one for loop to add one by one option to our list ( items ) by using its index position.
def my_add():
menu = om1["menu"]
last = menu.index("end")
items = []
for index in range(last+1):
items.append(menu.entrycget(index, "label"))
We will read the text entered by user. ( you can add any minimum length requirement here )
opt=t1.get("1.0",'end-1c') # read value entered by user
By using one if else condition check we can find out if the user entered data is already available as an option. If it is not there then we will add to the OptionMenu , otherwise we will give message saying already available.
if opt in items:
my_str.set("Already available")
else:
om1['menu'].add_command(label=opt, command=tk._setit(options, opt))
options.set(my_list[0]) # default value set
my_str.set("Option added")
Full code is here
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_list = ["PHP","MySQL","Python","HTML","JQuery"]
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value
l1 = tk.Label(my_w, text='Enter Option', width=10 ) # added one Label
l1.grid(row=2,column=1)
t1 = tk.Text(my_w, height=1, width=8,bg='yellow') # added one text box
t1.grid(row=2,column=2)
b1 = tk.Button(my_w, text='Add', command=lambda: my_add() )
b1.grid(row=2,column=4)
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5)
my_str = tk.StringVar()
l2 = tk.Label(my_w, text='Output',textvariable=my_str, width=20 ) # added one Label
l2.grid(row=3,column=1)
def my_add():
menu = om1["menu"]
last = menu.index("end")
items = []
for index in range(last+1):
items.append(menu.entrycget(index, "label"))
# items is the list with all available options of OptionMenu om1
#print(items)
opt=t1.get("1.0",'end-1c') # read value entered by user
if opt in items:
my_str.set("Already available")
else:
om1['menu'].add_command(label=opt, command=tk._setit(options, opt))
options.set(my_list[0]) # default value set
my_str.set("Option added")
my_w.mainloop()
User can remove all or add all the options to an OptionMenu. User can select any option and remove the same by Clicking a button.
Remove all or remove Selective options of an OptionMenu.
« OptionMenu
← Subscribe to our YouTube Channel here