6seven8
6seven8

Reputation: 97

How to set variable in Tkinter OptionMenu?

So my question is why the Tkinter OptionMenu is not setting to 4.5 as a default? Do I just have to put 4.5 at the beginning of the avgdisc_thresh list or am I doing something wrong?

avgdisc_thresh = [3.5, 4, 4.5, 5, 5.5, 6]

avgdisc_var = tk.StringVar()
avgdisc_var.set("4.5")

loss_option = ttk.OptionMenu(
    loss_top_win, avgdisc_var, *avgdisc_thresh)
loss_option.grid(
    row=0, column=0)

Thanks!

Upvotes: 1

Views: 2313

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385870

With the ttk OptionMenu, the first value after the name of the variable is used as the default, which overrides whatever value you had previously set. The simple solution is to provide the default value when creating the widget.

Example:

loss_option = ttk.OptionMenu(loss_top_win, avgdisc_var, "4.5", *avgdisc_thresh)

Upvotes: 3

Related Questions