Behseini
Behseini

Reputation: 6330

Calling function on changing combobox options in Tkinter

In the below code, how can I call the OptionCallBack function on cm changes and pass the selected option into the message box to be displayed?

import Tkinter
import tkMessageBox
from Tkinter import *
import ttk

app = Tk()
app.configure(background='DimGray')
app.geometry('600x600')
app.resizable(width=False, height=False)

def OptionCallBack():
   tkMessageBox.showinfo( "Selected Phase", "??????")



variable = StringVar(app)
variable.set("Select From List")

cm = ttk.Combobox(app, textvariable=variable)
cm.config(values =('Select From Phase A', 'Select From Phase B'))
cm.pack()

app.mainloop()

Upvotes: 3

Views: 3062

Answers (1)

Novel
Novel

Reputation: 13729

Use a trace on the variable:

def OptionCallBack(*args):
   tkMessageBox.showinfo( "Selected Phase", variable.get())

variable = StringVar(app)
variable.set("Select From List")
variable.trace('w', OptionCallBack)

Upvotes: 5

Related Questions