![change selected text color combobox ttk change selected text color combobox ttk](https://user-images.githubusercontent.com/44148347/50550211-7ef54200-0c20-11e9-97b6-0acbb2872885.png)
CHANGE SELECTED TEXT COLOR COMBOBOX TTK CODE
In above code we will add one if condition check inside the function my_insert() to check the string before adding. My_w.mainloop() # Keep the window open Preventing duplicate entry of option to Combobox
![change selected text color combobox ttk change selected text color combobox ttk](https://media.geeksforgeeks.org/wp-content/uploads/20190328010552/Adding_style_to_the_entered_text.jpg)
L1.config(text=sel.get() + ' : '+ str(cb1.current()))ĭef my_insert(): # adding data to ComboboxĬb1 = ttk.Combobox(my_w, values=months,width=7,textvariable=sel)ī1=tk.Button(my_w,text='Add', command=lambda: my_insert()) User can add data to Entry box and on click of the button b1 the user entered data will be added as option to the combobox cb1 through the function my_insert(). To the above code we will add one Entry box and Button. My_w.mainloop() # Keep the window open Adding Options to Combobox We used one String Variable connected to Combobox through textvariable optionĬb1 = ttk.Combobox(my_w, values=months,width=7, textvariable=sel) Here we are displaying the option value in one Label l1 once the selection is changed. Once option is selected or it is changed then we can trigger events to reflect the updation. L1.config(text=cb1.get()+':'+ str(cb1.current())) # value & index On Change event of Combobox
![change selected text color combobox ttk change selected text color combobox ttk](https://pythonlobby.com/wp-content/uploads/2020/10/combo-box-in-python-tkinter.jpg)
In above code we are shifting the selection on click of button by using cb1.set('Apr'), same way we can reset the selection to blank by using cb1.set('') or cb1.delete(0,'end') def my_upd1():Ĭb1.delete(0,'end') # clear the selection My_w.mainloop() # Keep the window open Clearing the selection of Combobox Months=Ĭb1 = ttk.Combobox(my_w, values=months,width=7)ī1=tk.Button(my_w,text="set('Apr')", command=lambda: my_upd1()) The output of cb1.current() is an integer so we used str() to convert the same to string before adding. Here on click of the button b1 the value of the Combobox cb1 is set to Apr then this value and index is collected and displayed in the Label l1 To get the index of the selected option we can use cb1.current() Example To get the selected value ( option ) we can use cb1.get() Getting value and index of selected option We can use the index of the option cb1.current(2) to set it as default selection. Months= # optionsĬb1 = ttk.Combobox(my_w, values=months,width=7) # ComboboxĬb1.grid(row=1,column=1,padx=10,pady=20) # adding to gridīy using cb1.set('Apr') we are setting a default selected option for the Combobox. My_w.geometry("300x150") # Size of the window Tkinter Combobox to select Option or add data by user & reading adding and setting default options