Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple ttk ComboBox demo

This should be very simple but I am really struggling to get it right. All I need is a simple ttk ComboBox which updates a variable on change of selection.

In the example below, I need the value of value_of_combo variable to be updated automatically every time a new selection is made.

from Tkinter import *
import ttk

class App:

    value_of_combo = 'X'


    def __init__(self, parent):
        self.parent = parent
        self.combo()

    def combo(self):
        self.box_value = StringVar()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value)
        self.box['values'] = ('X', 'Y', 'Z')
        self.box.current(0)
        self.box.grid(column=0, row=0)

if __name__ == '__main__':
    root = Tk()
    app = App(root)
    root.mainloop()
like image 740
bhaskarc Avatar asked Jul 20 '13 00:07

bhaskarc


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.

What is the difference between Tk and TTK?

Tkinter widgets are used to add Buttons, Labels, Text, ScrollBar, etc., however, tkinter. ttk supports a variety of widgets as compared to tkinter widgets. Tkinter. ttk doesn't support Place, Pack() and Grid(), thus it is recommended to use tkinter widget with ttk.

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.

How do I change the background color of a Combobox in Python?

We can set the background color, foreground color, and other attributes of the Combobox widget by visiting the configure function in ttk and passing 'TCombobox' as the first parameter.


2 Answers

Just bind the virtual event <<ComboboxSelected>> to the Combobox widget:

class App:
    def __init__(self, parent):
        self.parent = parent
        self.value_of_combo = 'X'
        self.combo()

    def newselection(self, event):
        self.value_of_combo = self.box.get()
        print(self.value_of_combo)

    def combo(self):
        self.box_value = StringVar()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value)
        self.box.bind("<<ComboboxSelected>>", self.newselection)
        # ...
like image 61
A. Rodas Avatar answered Oct 01 '22 06:10

A. Rodas


In the more general case, if you need to get the value of a Variable when it is updated, it would be advisable to use the tracing facility built into them.

var = StringVar()  # create a var object

# define the callback
def tracer(name, idontknow, mode):
    # I cannot find the arguments sent to the callback documented
    # anywhere, or how to really use them.  I simply ignore
    # the arguments, and use the invocation of the callback
    # as the only api to tracing
    print var.get()

var.trace('w', tracer)
# 'w' in this case, is the 'mode', one of 'r'
# for reading and 'w' for writing

var.set('Foo')  # manually update the var...

# 'Foo' is printed
like image 37
Tritium21 Avatar answered Oct 01 '22 04:10

Tritium21