Search code examples
pythontkintercombobox

How to set default value in the combobox in tkinter?


I am creating an application where I need a tkinter combox. There I want the combo box to have a default value from the starting of the application. I have tried current() method but it is not working.

Here is my code snipped

n= tk.StringVar()
youtubechoicesLabel = ttk.Combobox(root, font=font, justify='center', textvariable=n)
youtubechoicesLabel['values'] = ("----Download Type----",
                                    "Mp4  720p",
                                    "Mp4  144p",
                                    "Video  3gp",
                                    "Audio  Mp3")

youtubechoicesLabel.current(0)
youtubechoicesLabel["background"] = '#ff0000'
youtubechoicesLabel["foreground"] = '#ffffff'
youtubechoicesLabel.pack(side=TOP, pady=20)

Solution

  • Calling current() is correct and it is working — you just can't see the current selection due to the use the white foreground color you specified.

    n = tk.StringVar()
    youtubechoicesLabel = ttk.Combobox(root, font=font, justify='center', textvariable=n)
    youtubechoicesLabel['values'] = ("----Download Type----",
                                        "Mp4  720p",
                                        "Mp4  144p",
                                        "Video  3gp",
                                        "Audio  Mp3")
    
    youtubechoicesLabel.current(0)
    youtubechoicesLabel["background"] = '#ff0000'
    #youtubechoicesLabel["foreground"] = '#ffffff'  # <----- DISABLED
    youtubechoicesLabel.pack(side=TOP, pady=20)