- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to clear the text field part of ttk.Combobox in tkinter?
The Combobox widget is one of the versatile widgets in tkinter that is used to create a dropdown list containing some values in it. You can select a value from the dropdown list that gets replaced by the default value of the combobox widget. You can create a combobox widget by initializing the constructor of Combobox(root, width, text) widget.
Consider the case, if the user wants to clear the selected value from the combobox widget, the only way you can do so is to set the value of the combobox widget as NULL by using the set (' ') method. The following example demonstrates how to do it.
Example
# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win = Tk() # Set the size of the window win.geometry("700x350") # Create a function to clear the combobox def clear_cb(): cb.set('') # Define Days Tuple days= ('Sun','Mon','Tue','Wed','Thu','Fri','Sat') # Create a combobox widget var= StringVar() cb= ttk.Combobox(win, textvariable= var) cb['values']= days cb['state']= 'readonly' cb.pack(fill='x',padx= 5, pady=5) # Create a button to clear the selected combobox text value button = Button(win, text= "Clear", command= clear_cb) button.pack() win.mainloop()
Output
Running the above code will display a window with a combobox widget and a button "Clear" to clear the selected value from the combobox widget.
Now click on the "Clear" button to clear the selected value from the combobox widget.
- Related Articles
- How to clear the contents of a Tkinter Text widget?
- How to set the background color of a ttk.Combobox in tkinter?
- How can I disable typing in a ttk.Combobox tkinter?
- How to clear Text widget content while clicking on the Entry widget itself in Tkinter?
- How to clear Tkinter Canvas?
- How to clear out a frame in the Tkinter?
- How to clear a Tkinter ListBox?
- How to word-wrap text in Tkinter Text?
- How to insert text at the beginning of the text box in Tkinter?
- How to clear an entire Treeview with Tkinter?
- How to clear the text entered in Selenium with python?
- How to get the current length of the Text in a Tkinter Text widget?
- How to highlight text in a tkinter Text widget?
- How to get the Tkinter Label text?
- How to align text to the left in Tkinter Label?
