SOHAM DAS BISWAS.
SOHAM DAS BISWAS.

Reputation: 71

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)

Upvotes: 2

Views: 5984

Answers (3)

Abdullah Al Nahian
Abdullah Al Nahian

Reputation: 1

you need to disable foreground color and bind an event to your combobox. I had the same issue and the above solutions didn't work for me. I fixed it by binding an event to it.

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

youtubechoicesLabel["background"] = '#ff0000'
#youtubechoicesLabel["foreground"] = '#ffffff' #disable it as martineau said
youtubechoicesLabel.pack(side=tk.TOP, pady=20)
youtubechoicesLabel.current(0)

#bind an event to your youtubechoicesLabel
def ComboboxEvent(event):
    print("some event")

youtubechoicesLabel.bind("<<ComboboxSelected>>", ComboboxEvent)

Upvotes: 0

martineau
martineau

Reputation: 123413

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)

Upvotes: 3

Novel
Novel

Reputation: 13729

Just set the value of n.

n.set('default value')

Upvotes: 2

Related Questions