tkinter optionmenu first option vanishes

前端 未结 5 1764
面向向阳花
面向向阳花 2020-12-05 08:00

A ttk optionmenu widget starts out with all of its values in the dropdown. Upon selecting any value, the first value in the list vanishes, never to reappear...

Does

5条回答
  •  有刺的猬
    2020-12-05 08:50

    Old post, but I figured I would add to this conversation. A very simple way to make the first (or any item in your list for that matter) display by default on a Mac when using tk.OptionMenu is to utilize tk.StringVar For example:

    options = ['option1', 'option2', 'option3', 'option4']
    root = Tk()
    var = StringVar()
    var.set(options[0])
    OptionMenu(root, var, *options)
    mainloop()
    
    selected = var.get()
    

    It's critical that you use var.set in order to establish what is the 'set' value. Hope that helps!

提交回复
热议问题