tkinter optionmenu first option vanishes

时间秒杀一切 提交于 2019-11-30 02:05:00

The signature of the ttk.OptionMenu command is this:

def __init__(self, master, variable, default=None, *values, **kwargs):

This is the docstring:

"""Construct a themed OptionMenu widget with master as the parent, the resource textvariable set to variable, the initially selected value specified by the default parameter, the menu values given by *values and additional keywords.

Notice the default option which comes before the list of values. Instead of adding a blank item to the list of values, add whichever value you want as the default:

options = ['1', '2', '3']
dropdown = ttk.OptionMenu(masterframe, value, options[1], *options)
Pedro Moresco

just adding to the answers of the other guys, since they didn't work for me. I discovered that if you don't set the widget option in StringVar/IntVar, it doesn't show the standard value that's been set. It might seem silly but it took me a lot of time to figure this out. Hope it helps, see ya. Example:

master = tk.Tk()
var = tk.StringVar(master)
master.mainloop()

It seems that with ttk.OptionMenu the first entry in the options list must be left blank:

import tkinter.ttk as ttk
import tkinter as tk

a = tk.Tk()

options = ['', '1', '2', '3']
value = tk.StringVar()
value.set(options[1])

masterframe = ttk.Frame()
masterframe.pack()

dropdown = ttk.OptionMenu(masterframe, value, *options)
dropdown.pack()

a.mainloop()

Source: http://www.pyinmyeye.com/2012/08/tkinter-menubutton-demo.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!