Add Itmes in ComboBox in Tkinter Python with Loop Example

Here, we will discuss and Learn about Tkinter Combobox in Python. Here we will learn how to Add Items in Combobox in Tkinter or Add Items in ComboBox in Tkinter through Loop in Python. So, we will proceed with step by step Process with following points.


  1. Manually Add Items in ComboBox Tkinter in python
  2. Add Items in ComboBox Tkinter in python through Loop

Now, lets Learn point wise given bellow.

 How To Get value From Textbox to Label in Tkinter in Python?

(1). Manually Add Items in ComboBox Tkinter in python :

If you want to add items in design time or manually then you have to write following code in Python. Tkinter provide ttk class to show combobox and its attributes. So, Manually add items process code in tkinter described in following way:
import tkinter as tk
from tkinter import *
from tkinter import ttk

MyrootDialog = tk.Tk(className="Add Items in Tkinter Combobox")
#Set Tkinter window Size
MyrootDialog.geometry("300x100")

mytkcombo = ttk.Combobox(MyrootDialog, values=['SKO', 'Tech', 'Learn'])
#Set Combobox Position in Python
mytkcombo.place(relx=0.5, rely=0.3, anchor='center')
mytkcombo['state'] = 'readonly'
#mycombo.pack()

MyrootDialog.mainloop()
When you run this code, this will show you following output.

Manually Show items in Combobox Tkinter
Items in Tkinter ComboBox in Python


So, In this way you can add items in ComboBox tkinter in Python through static way. Now let’s Learn about dynamic way at run time.


(2). Add Items in ComboBox Tkinter in python through Loop :

Now, if You want to add itmes in tkinter combobox at runtime with loop or loop to add items in tkinter combobox in python, you have to implement some code in python like bellow: 

Example 1:
import tkinter as tk
from tkinter import *
from tkinter import ttk

MyrootDialog = tk.Tk(className="Add Items in Tkinter Combobox")
#Set Tkinter window Size
MyrootDialog.geometry("300x100")

mytkcombo = ttk.Combobox(MyrootDialog)
# For Loop
for x in range(5):
   # Add itmes in combobox through Loop code
   mytkcombo['values']= tuple(list(mytkcombo['values']) + [str(x)])
mytkcombo.place(relx=0.5, rely=0.3, anchor='center')

MyrootDialog.mainloop()

This is the dynamic way to show items in combobox at run time through loop. This will show following output:
Loop Items in ComboBox Tkinter
Loop Items in ComboBox



Show Month in Combobox Tkinter through Loop

Example 2:

Now, lets See another Example to showing month name in ComboBox through Loop . In this example we use array list to add items in ComboBox Python.
import tkinter as tk
from tkinter import *
from tkinter import ttk
import calendar

MyrootDialog = tk.Tk(className="Add Items in Tkinter Combobox")
#Set Tkinter window Size
MyrootDialog.geometry("300x100")

mytkcombo = ttk.Combobox(MyrootDialog)
# Define Array List
strList=['']
for mnt in range(1, 13):
   #Add Month Name through Loop
   strList.append(calendar.month_name[mnt])
#Add Items in Tkinter Combobox through Array
mytkcombo['values'] = strList
mytkcombo.place(relx=0.5, rely=0.3, anchor='center')

MyrootDialog.mainloop()




After running this code you will see the output like following way:

Show Month In ComboBox in Tkinter
Show Month Name in Tkinter ComboBox in Python


So, these are the three way to show or Add Items in Combobox in tkinter in Python, you can choose anyone according to your application or software requirement.

0 comments: