Number of Text box based selection of Combobox

Number of textbox based on Combobox selection

User has to select one of the two available options of the Combobox.
Based on the selection either two or three text boxes are to be displayed.

Based on Selection of option from Combobox display two or three text boxes in Tkinter window
import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x150")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title
font1=('Times',18,'normal')
sel=tk.StringVar() # string variable 
my_opts=['Show Two','Show Three'] # options
cb1 = ttk.Combobox(my_w, values=my_opts,width=25,
        textvariable=sel,font=font1) # Combobox
cb1.grid(row=0,column=0,padx=10,pady=20,columnspan=3) # adding to grid
def my_upd(*args):
    for w in my_w.grid_slaves(1): # all elements 
        w.grid_remove()                  # delete elements 
    if(sel.get()=='Show Two'):
        t1 = tk.Text(my_w,  height=1, width=8,bg='yellow') # 
        t1.grid(row=1,column=0,padx=10,pady=20)
        t2 = tk.Text(my_w,  height=1, width=8,bg='yellow') # 
        t2.grid(row=1,column=1,padx=10,pady=20)
    elif(sel.get()=='Show Three'):
        t1 = tk.Text(my_w,  height=1, width=8,bg='yellow') # 
        t1.grid(row=1,column=0,padx=1,pady=20)
        t2 = tk.Text(my_w,  height=1, width=8,bg='yellow') # 
        t2.grid(row=1,column=1,padx=1,pady=20)
        t3 = tk.Text(my_w,  height=1, width=8,bg='yellow') # 
        t3.grid(row=1,column=2,padx=1,pady=20)   
sel.trace('w',my_upd) # on change of string variable 
my_w.mainloop()  # Keep the window open
Python Tkinter Projects
Tkinter Text Tkinter Entry How to Validate user entered data
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate 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.



Subscribe to our YouTube Channel here



plus2net.com







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 Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer