How to make a ttk.Combobox callback How to make a ttk.Combobox callback tkinter tkinter

How to make a ttk.Combobox callback


You can bind to the <<ComboboxSelected>> event which will fire whenever the value of the combobox changes.

def TextBoxUpdate(event):    ...FriendListComboBox.bind("<<ComboboxSelected>>", TextBoxUpdate)


Use IntVar and StringVar .

You can use the trace method to attach “observer” callbacks to the variable. The callback is called whenever the contents change:

import Tkinterimport ttkFriendMap = {}UI = Tkinter.Tk()UI.geometry("%dx%d+%d+%d" % (330, 80, 500, 450))UI.title("User Friend List Lookup")def TextBoxUpdate():    if not  FriendListComboBox.get() == "":        FriendList = FriendMap[FriendListComboBox.get()]        FriendListBox.insert(0,FriendMap[UserListComboBox.get()])`def calback():    print("do something")#Imports the data from the FriendList.txt filewith open("C:\Users\me\Documents\PythonTest\FriendList.txt", "r+") as file:for line in file:    items = line.rstrip().lower().split(":")    FriendMap[items[0]] = items[1]#Creates a dropdown box with all of the keys in the FriendList filevalue = StringVar()value.trace('w', calback)FriendListKeys = FriendMap.keys()FriendListKeys.sort()FriendListComboBox =   ttk.Combobox(UI,values=FriendListKeys,command=TextBoxUpdate,textvariable=value)`

the callback function will be called when the comobox changes