
| Component ( Text ) | Function | Action performed by the Function |
|---|---|---|
| b1 ( Remove All ) | my_remove() | Removes all the options of the OptionMenu |
| b2 ( Add All Opt) | my_add() | Add all the options to the OptionMenu |
| b3 (Remove Selected) | my_remove_sel() | Removes selected Option |
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_list = ["PHP","MySQL","Python","HTML","JQuery"]
my_list.sort() # sort the list in alphabetical order
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=4)
b1 = tk.Button(my_w, text='Remove All', command=lambda: my_remove() )
b1.grid(row=1,column=2)
b2 = tk.Button(my_w, text='Add All Opt', command=lambda: my_add() )
b2.grid(row=2,column=2)
b3 = tk.Button(my_w, text='Remove Selectd', command=lambda: my_remove_sel() )
b3.grid(row=3,column=2)
Each of the three Buttons we used here are associated to three different functions ( command=lambda: my_add()).
def my_remove():
options.set('') # remove default selection only, not the full list
om1['menu'].delete(0,'end') # remove full list
def my_add():
my_remove() # remove all options
for opt in my_list:
om1['menu'].add_command(label=opt, command=tk._setit(options, opt))
options.set(my_list[0]) # default value set
def my_remove_sel():
r_index=om1['menu'].index(options.get()) # index of selected option.
om1['menu'].delete(r_index) # deleted the option
options.set(om1['menu'].entrycget(0,"label")) # select the first one
Here is the full code.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_list = ["PHP","MySQL","Python","HTML","JQuery"]
my_list.sort() # sort the list in alphabetical order
options = tk.StringVar(my_w) # to hold the user selection of option
options.set(my_list[0]) # default value
om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=2,column=5)
b1 = tk.Button(my_w, text='Remove All', command=lambda: my_remove() )
b1.grid(row=1,column=2)
b2 = tk.Button(my_w, text='Add All Opt', command=lambda: my_add() )
b2.grid(row=2,column=2)
b3 = tk.Button(my_w, text='Remove Selectd', command=lambda: my_remove_sel() )
b3.grid(row=3,column=2)
#om1['menu'].insert_separator(2) # add separator after 2nd element
def my_remove(): # remove all options
options.set('') # remove default selection only, not the full list
om1['menu'].delete(0,'end') # remove full list
def my_add():
my_remove() # remove all options
for opt in my_list:
om1['menu'].add_command(label=opt, command=tk._setit(options, opt))
options.set(my_list[0]) # default value set
def my_remove_sel(): # remove the user selected option
r_index=om1['menu'].index(options.get())
om1['menu'].delete(r_index)
options.set(om1['menu'].entrycget(0,"label")) # select the first one
my_w.mainloop()

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.