tkinter optionmenu first option vanishes

前端 未结 5 1784
面向向阳花
面向向阳花 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:35

    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

提交回复
热议问题