Set a default value for a ttk Combobox

73,397

Solution 1

The problem is that the instance of StringVar is getting garbage-collected. This is because it's a local variable due to how you wrote your code.

One solution is to use a class so that your StringVar persists:

from tkinter import Tk, StringVar, ttk

class Application:

    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, 
                                state='readonly')
        self.box['values'] = ('A', 'B', 'C')
        self.box.current(0)
        self.box.grid(column=0, row=0)

if __name__ == '__main__':
    root = Tk()
    app = Application(root)
    root.mainloop()

Solution 2

When your function 'combo' exits, the local variable 'value' is destroyed. You need a persistent variable, such as a global variable or a variable that is a property of a class so that the value isn't garbage-collected while the widget still exists.

Solution 3

The get() method can be used within your function to rename the StringVar and save it under another name to avoid losing it altogether via garbage collection.

value = StringVar()

keepvalue = value.get()

then use keepvalue instead of value:

box = ttk.Combobox(root, textvariable=keepvalue, state='readonly')

This had 'A' showing in the combobox for me.

Share:
73,397
kynikos
Author by

kynikos

Updated on April 04, 2020

Comments

  • kynikos
    kynikos about 4 years

    I'm using Python 3.2.1 in Arch Linux x86_64. This one is really driving me crazy: I just want to have a default, preselected value for a ttk.Combobox as soon as I grid it. This is my code:

    from tkinter import Tk, StringVar, ttk
    
    root = Tk()
    
    def combo(parent):
        value = StringVar()
        box = ttk.Combobox(parent, textvariable=value, state='readonly')
        box['values'] = ('A', 'B', 'C')
        box.current(0)
        box.grid(column=0, row=0)
    
    combo(root)
    
    root.mainloop()
    

    Which draws an empty Combobox. What's funny is that if I don't use a function it works perfectly:

    from tkinter import Tk, StringVar, ttk
    
    root = Tk()
    
    value = StringVar()
    box = ttk.Combobox(root, textvariable=value, state='readonly')
    box['values'] = ('A', 'B', 'C')
    box.current(0)
    box.grid(column=0, row=0)
    
    root.mainloop()
    

    Of course, in the real program I have to use a function, so I need another solution.

  • kynikos
    kynikos almost 13 years
    Thank you, it works perfectly! Actually I was using a class in my app, but I was using a local variable for box_value; with self.box_value it does the trick ;)
  • kynikos
    kynikos almost 13 years
    Thanks for the technical explanation ^^
  • texasman1979
    texasman1979 almost 9 years
    Sorry to jump in, but when i import ttk, It doesn't recognize "Combobox" as a option or widget. Any ideas?
  • Eryk Sun
    Eryk Sun almost 9 years
    @texasman1979, which version of Python are you using, and which OS? Check ttk.__file__ to make sure it's the right ttk module.
  • Jalkhov
    Jalkhov over 3 years
    Its possible set the current item by value? Example: self.box.current("Dubai")