Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a ttk.Combobox callback

I was wondering if there was a way to invoke a call back from a ttk.Combobox when the user selects an item from the drop-down list. I want to check to see what the value of the combobox is when an item is clicked so that I can display the associated dictionary value given 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 doesn't work since there is no 'command' for Comboboxes but I am not really sure what I need to do here to get that to work. Any help would be appreciated.

like image 822
Marek Avatar asked Feb 04 '16 19:02

Marek


People also ask

How do I use TTK Combobox?

Use ttk. Combobox(root, textvariable) to create a combobox. Set the state property to readonly to prevent users from entering custom values. A combobox widget emits the '<<ComboboxSelected>>' event when the selected value changes.

How do I clear my TTK Combobox?

You can create a combobox widget by initializing the constructor of Combobox(root, width, text) widget. Consider the case, if the user wants to clear the selected value from the combobox widget, the only way you can do so is to set the value of the combobox widget as NULL by using the set (' ') method.

How do you bind a Combobox in Python?

Import the Module The combobox widget is a class of the ttk module of the Tkinter library. To start using Ttk, first import the Ttk module and follow the Tk module. By this way, we override the widgets of the Tk module with the Ttk module. The ttk module provides a better look and feel among many platforms.

How do you make a Combobox not editable in Python?

We can select any option in the dropdown list. Now add state = "readonly" in the Combobox object, it will make the Combobox Entry Widget disabled.


2 Answers

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

def TextBoxUpdate(event):
    ...
FriendListComboBox.bind("<<ComboboxSelected>>", TextBoxUpdate)
like image 161
Bryan Oakley Avatar answered Sep 23 '22 14:09

Bryan Oakley


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 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)`

the callback function will be called when the comobox changes

like image 24
Mostafa Wattad Avatar answered Sep 22 '22 14:09

Mostafa Wattad