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 ) |
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()
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()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.