Set a default value for a ttk Combobox in Tkinter?

PythonTkinterServer Side ProgrammingProgramming

Tkinter Combobox is used to add a drop-down menu to the Entry widget, making it useful to handle multiple data of any application. A Combobox widget can be created using the Combobox(arguments). However, for the particular need of an application, we can set the default value for the Combobox widget. It can be set by listing all the records in a variable that needs to be present in the Combobox. By specifying the index of the particular value in the current(index) method, we can set the default value in the Combobox widget.

Example

#Import Tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Create a Combobox
combobox= ttk.Combobox(win,state= "readonly")
combobox['values']=('C++','Java','Python')
combobox.current(2)
combobox.pack(pady=30, ipadx=20)
win.mainloop()

Output

Running the above code will display a window that contains a Combobox with some default value. We can change the default value by changing the index of the current method.

raja
Published on 22-Apr-2021 07:46:10
Advertisements