OptionMenu add remove options


Add remove options of OptionMenu

Components in our GUI window

Our Tkinter GUI will have three Buttons and one OptionMenu.
To keep it simple we will not add more interlocks to the process like checking the existing options before adding.

Each button will execute a function associated with Click event of it. Each function perform the respective action. Here is a list of Buttons, the function name and action to perform.
Component ( Text ) FunctionAction 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

Create the window and add components

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"]
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()).

my_remove()

It deletes the default selection first and then it removes all the options.

my_add()

First it removes all the option by calling the function my_remove(). Then one for loop is used for the list to show all elements and those elements are added to the OptionMenu.

my_remove_sel()

First we found out the index of the selected item. This index is used to remove ( delete ) the option. Finally the default selection of the OptionMenu is shifted to first element.
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"]
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=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)

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())
    om1['menu'].delete(r_index)
    options.set(om1['menu'].entrycget(0,"label")) # select the first one 
    
my_w.mainloop()

Adding user entered text as Option of an OptionMenu.
OptionMenu
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer