How To Get Selected Item Value From A Tkinter Combobox Using Python
In This Python Tkinter Tutorial we will See How To Get The Value Of The Selected Item From a Tkinter combobox On ComboboxSelected Event Using Python.
Source Code:
import tkinter as tk
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Get Combobox Value")
frame = tk.Frame(root, padx=40,pady=40, bg='grey')
options = ["Python","C#","Java","Php","C++","VB.Net","JavaScript"]
selected = StringVar(frame)
selected.set(options[0])
combobox = ttk.Combobox(frame, textvariable=selected, values=options,
font=('verdana',20))
label = tk.Label(frame, text="Username:", bg="grey",fg="#fff", font=("Verdana",20))
input_entry = tk.Entry(frame, width=20, font=("Verdana",20))
def getSelectedItem(eventObject):
print(combobox.get())
label['text'] = combobox.get()
input_entry.delete(0,END)
input_entry.insert(0,combobox.get())
combobox.bind("<<ComboboxSelected>>", getSelectedItem)
combobox.grid(row=0, column=0)
label.grid(row=1, column=0)
input_entry.grid(row=2, column=0)
frame.pack()
root.mainloop()
OUTPUT: