Combobox In Tkinter
Combobox in Tkinter .
OUTPUT
Thank you and leave a feedback comment .......
Combobox in tkinter is a dropdown list which can hold multiple values and show one item at a time.
Combobox is useful for selecting one option in many choice .
Combobox is a ttk class widget so before using Combobox in our code we also have to import ttk .
from tkinter import ttk from tkinter import * root = Tk() # LIST OF COURSES course = ['Python','Java','C','C++','PhP','sql'] # create a comboboox of name cb cb = ttk.Combobox(root,values=course,width=10) cb.pack(pady=10) root.mainloop()
In the Above code we import tkinter and its all class including ttk and create a list of values which we want to show in our Combobox .
next we create our Combobox named cb and place it on root and use our list as its elements by placing the list equal to values attribute.
the width defines maximum character to show in our Combobox..
OUTPUT
The above code create a Combobox like this .
How to fetch and set value at Combobox .
from tkinter import ttk from tkinter import * root = Tk() def func(): print(cb.get()) # LIST OF COURSES course = ['Python','Java','C','C++','PhP','sql'] # create a comboboox of name cb cb = ttk.Combobox(root,values=course,width=10) cb.pack(pady=10) cb.set("Java") # you can use other ways to setting default values on Combobox like. # cb.set(course[1]) # cb.current(1) btn = Button(root,text="get Values",command= func) btn.pack(pady=20) root.mainloop()
We can use all the 3 ways for setting value at Combobox as show in code and comment section .
and for fetching current Combobox data we can use .get( ) method .
Some Basic attributes of Combobox .
Sr.No. | Syntax & Description |
---|---|
1 | backgroundcolor / bg Used to set background color for widget. |
2 | borderwidth / bd Used to draw with border in 3D effects. |
3 | font Used to set font for widget. |
4 | foregroundcolor / fg Used to set foreground color for widget. |
5 | textvariable Variable associated with the widget. When the text of widget changes, the variable is set to text of widget. |
6 | values array Arbitrary values for combobox widget. |
7 | width Sets the width for widget. |
8 | justify Sets the alignment of text, which can be left, center, or right. |
9 | state Sets the state, which can be read_only, disabled, or normal. |
This post is contributed by Yogesh singh .
You can check out our more blog on tkinter and stuffs.
and .
Thank you and leave a feedback comment .......
No comments