How to set variable in Tkinter OptionMenu?

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?


OptionMenu


4.5


4.5


avgdisc_thresh


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!






In line 6, is ttk a typo? Do you mean tk.OptionMenu...?

– rcriii
Sep 9 '18 at 22:59






No. ttk has some more stylish menus, etc than tk. I use it wherever it works

– 6seven8
Sep 9 '18 at 23:15







Ahh. I see. they don't work together. had to use tk.. thanks!

– 6seven8
Sep 9 '18 at 23:16




1 Answer
1



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.


OptionMenu



Example:


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






So either I use tk or change the format of my parameters? I had not realized ttk had a separate syntax

– 6seven8
Sep 9 '18 at 23:21


tk


ttk






I am trying to figure out why they would do this - it seems redundant to set a variable and then have the variable changed by the default value. Maybe you could help me understand?

– 6seven8
Sep 9 '18 at 23:25







@6seven8: not really. The Tkinter OptionMenu also takes the default value as the first parameter. In Tkinter it's a positional parameter, in ttk it's a keyword parameter.

– Bryan Oakley
Sep 9 '18 at 23:26


OptionMenu


Tkinter



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)