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.
- Manually Add Items in ComboBox Tkinter in python
- Add Items in ComboBox Tkinter in python through Loop
How To Get value From Textbox to Label in Tkinter in Python?
(1). Manually Add Items in ComboBox Tkinter in python :
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.
Items in Tkinter ComboBox in Python |
(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:
This is the dynamic way to show items in combobox at run time through loop. This will show following output: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()
Loop Items in ComboBox |
Show Month in Combobox Tkinter through Loop
Example 2:
Example 2:
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()
Show Month Name in Tkinter ComboBox in Python |
0 comments: