How to make ttk.Combobox callback

I was wondering if there is a way to trigger a call from ttk.Combobox when the user selects an item from the drop-down list. I want to check what value the combobox value is when I click on an element so that I can display the corresponding dictionary value using the combobox key.

import Tkinter
import ttk

FriendMap = {}
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[FriendListComboBox.get()])`

#Imports the data from the FriendList.txt file
with 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 file
FriendListKeys = FriendMap.keys()
FriendListKeys.sort()
FriendListComboBox = ttk.Combobox(UI,values=FriendListKeys,command=TextBoxUpdate)`

The last line obviously does not work, since there is no command for Comboboxes, but I'm not quite sure what I need to do here to make it work. Any help would be appreciated.

+4
source share
2 answers

<<ComboboxSelected>>, , combobox.

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

IntVar StringVar.

trace . , :

import Tkinter
import ttk

FriendMap = {}
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 file
with 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 file
value = StringVar()
value.trace('w', calback)
FriendListKeys = FriendMap.keys()
FriendListKeys.sort()
FriendListComboBox =   ttk.Combobox(UI,values=FriendListKeys,command=TextBoxUpdate,textvariable=value)`

, comobox

+1

Source: https://habr.com/ru/post/1627343/


All Articles