tkinter optionmenu first option vanishes












12















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 anyone know why? Is this a feature of the widget's design? Try it with the following:



import tkinter.ttk as ttk
import tkinter as tk

a = tk.Tk()

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

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

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

a.mainloop()


Note - another user asked the same question here: OptionMenu won't show the first option when clicked (Tkinter)



They seem to've found a workaround, but not understood why it was happening.



UPDATE: actually this behaviour only appears when using the ttk widget. The tk widget works fine (albeit looking very ugly).










share|improve this question





























    12















    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 anyone know why? Is this a feature of the widget's design? Try it with the following:



    import tkinter.ttk as ttk
    import tkinter as tk

    a = tk.Tk()

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

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

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

    a.mainloop()


    Note - another user asked the same question here: OptionMenu won't show the first option when clicked (Tkinter)



    They seem to've found a workaround, but not understood why it was happening.



    UPDATE: actually this behaviour only appears when using the ttk widget. The tk widget works fine (albeit looking very ugly).










    share|improve this question



























      12












      12








      12


      7






      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 anyone know why? Is this a feature of the widget's design? Try it with the following:



      import tkinter.ttk as ttk
      import tkinter as tk

      a = tk.Tk()

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

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

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

      a.mainloop()


      Note - another user asked the same question here: OptionMenu won't show the first option when clicked (Tkinter)



      They seem to've found a workaround, but not understood why it was happening.



      UPDATE: actually this behaviour only appears when using the ttk widget. The tk widget works fine (albeit looking very ugly).










      share|improve this question
















      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 anyone know why? Is this a feature of the widget's design? Try it with the following:



      import tkinter.ttk as ttk
      import tkinter as tk

      a = tk.Tk()

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

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

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

      a.mainloop()


      Note - another user asked the same question here: OptionMenu won't show the first option when clicked (Tkinter)



      They seem to've found a workaround, but not understood why it was happening.



      UPDATE: actually this behaviour only appears when using the ttk widget. The tk widget works fine (albeit looking very ugly).







      python tkinter ttk optionmenu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 11 '18 at 20:22









      Mr. T

      4,18791535




      4,18791535










      asked Oct 2 '13 at 13:50









      TomTom

      126315




      126315
























          3 Answers
          3






          active

          oldest

          votes


















          24














          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)





          share|improve this answer





















          • 5





            haha, great, you added a good answer to a 1-year old question exactly 4 hours before I needed that.

            – Gregor
            Aug 9 '14 at 15:24



















          1














          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






          share|improve this answer
























          • That link appears to be dead.

            – Bryan Oakley
            Aug 9 '14 at 11:03






          • 3





            While your workaround works, it's not a very good description of the problem. ttk.OptionMenu expects a default value after the variable argument and before the list of values. While your solution works, it obscurs what is really happening.

            – Bryan Oakley
            Aug 9 '14 at 11:19











          • Hahaha, I like this a lot!!

            – StackG
            Jun 17 '18 at 11:05



















          0














          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()





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19138534%2ftkinter-optionmenu-first-option-vanishes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            24














            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)





            share|improve this answer





















            • 5





              haha, great, you added a good answer to a 1-year old question exactly 4 hours before I needed that.

              – Gregor
              Aug 9 '14 at 15:24
















            24














            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)





            share|improve this answer





















            • 5





              haha, great, you added a good answer to a 1-year old question exactly 4 hours before I needed that.

              – Gregor
              Aug 9 '14 at 15:24














            24












            24








            24







            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)





            share|improve this answer















            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)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 17 '14 at 21:57

























            answered Aug 9 '14 at 11:18









            Bryan OakleyBryan Oakley

            216k22258421




            216k22258421








            • 5





              haha, great, you added a good answer to a 1-year old question exactly 4 hours before I needed that.

              – Gregor
              Aug 9 '14 at 15:24














            • 5





              haha, great, you added a good answer to a 1-year old question exactly 4 hours before I needed that.

              – Gregor
              Aug 9 '14 at 15:24








            5




            5





            haha, great, you added a good answer to a 1-year old question exactly 4 hours before I needed that.

            – Gregor
            Aug 9 '14 at 15:24





            haha, great, you added a good answer to a 1-year old question exactly 4 hours before I needed that.

            – Gregor
            Aug 9 '14 at 15:24













            1














            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






            share|improve this answer
























            • That link appears to be dead.

              – Bryan Oakley
              Aug 9 '14 at 11:03






            • 3





              While your workaround works, it's not a very good description of the problem. ttk.OptionMenu expects a default value after the variable argument and before the list of values. While your solution works, it obscurs what is really happening.

              – Bryan Oakley
              Aug 9 '14 at 11:19











            • Hahaha, I like this a lot!!

              – StackG
              Jun 17 '18 at 11:05
















            1














            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






            share|improve this answer
























            • That link appears to be dead.

              – Bryan Oakley
              Aug 9 '14 at 11:03






            • 3





              While your workaround works, it's not a very good description of the problem. ttk.OptionMenu expects a default value after the variable argument and before the list of values. While your solution works, it obscurs what is really happening.

              – Bryan Oakley
              Aug 9 '14 at 11:19











            • Hahaha, I like this a lot!!

              – StackG
              Jun 17 '18 at 11:05














            1












            1








            1







            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






            share|improve this answer













            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 7 '13 at 18:18









            J. NunesJ. Nunes

            271




            271













            • That link appears to be dead.

              – Bryan Oakley
              Aug 9 '14 at 11:03






            • 3





              While your workaround works, it's not a very good description of the problem. ttk.OptionMenu expects a default value after the variable argument and before the list of values. While your solution works, it obscurs what is really happening.

              – Bryan Oakley
              Aug 9 '14 at 11:19











            • Hahaha, I like this a lot!!

              – StackG
              Jun 17 '18 at 11:05



















            • That link appears to be dead.

              – Bryan Oakley
              Aug 9 '14 at 11:03






            • 3





              While your workaround works, it's not a very good description of the problem. ttk.OptionMenu expects a default value after the variable argument and before the list of values. While your solution works, it obscurs what is really happening.

              – Bryan Oakley
              Aug 9 '14 at 11:19











            • Hahaha, I like this a lot!!

              – StackG
              Jun 17 '18 at 11:05

















            That link appears to be dead.

            – Bryan Oakley
            Aug 9 '14 at 11:03





            That link appears to be dead.

            – Bryan Oakley
            Aug 9 '14 at 11:03




            3




            3





            While your workaround works, it's not a very good description of the problem. ttk.OptionMenu expects a default value after the variable argument and before the list of values. While your solution works, it obscurs what is really happening.

            – Bryan Oakley
            Aug 9 '14 at 11:19





            While your workaround works, it's not a very good description of the problem. ttk.OptionMenu expects a default value after the variable argument and before the list of values. While your solution works, it obscurs what is really happening.

            – Bryan Oakley
            Aug 9 '14 at 11:19













            Hahaha, I like this a lot!!

            – StackG
            Jun 17 '18 at 11:05





            Hahaha, I like this a lot!!

            – StackG
            Jun 17 '18 at 11:05











            0














            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()





            share|improve this answer






























              0














              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()





              share|improve this answer




























                0












                0








                0







                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()





                share|improve this answer















                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()






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 2:39









                Marcus Campbell

                2,01921127




                2,01921127










                answered Nov 21 '18 at 1:18









                Pedro MorescoPedro Moresco

                214




                214






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


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




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19138534%2ftkinter-optionmenu-first-option-vanishes%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith