How can I dynamically update ttk.combobox?












0















I am creating a GUI using Python 3.4 and Tkinter on a Windows 8 computer.



The GUI has some Entry inputs at the top, then some comboboxes. I want the combobox to acquire a list of options from a text file that is described by the previous inputs (file name, the row in which the requisite information is found, delimiter type, etc.). I am trying to use the postcommand, but it seems to run first thing and never update, rather then update every time I access the drop down menu of the combobox.



    datatypes = 
datatypes = ttk.Combobox(tab_loc, textvariable=std1, values=datatypes, postcommand=self.get_datatypes(datatypes,
self.flnm2.get(), self.hl2_text.get(), self.delim2.get(), self.fcd2_text.get())).grid(pady=v_pad,
padx=h_pad, row=8, column=1, sticky=EW)

def get_datatypes(self, lst, flnm, hl, delim, fcd):
# Problem: postcommand runs at start of GUI. Prevents updating.
lst += ["test", "worked?"]
print("stuff")
lst += flnm
lst += hl
try:
# open the file, get the line, break it apart.
except:
pass


self.flnm2, self.hl2_text, self.delim2, and self.fcd2_text are some of the previous inputs. They are StringVar.



When I run this code, the combobox has the options test, worked?, and two blank lines (presumably for flnm and hl). I plan to have multiple comboboxes like this, just with different inputs, so I need a function I can give inputs to and then updates datatypes.



What am I doing wrong?










share|improve this question





























    0















    I am creating a GUI using Python 3.4 and Tkinter on a Windows 8 computer.



    The GUI has some Entry inputs at the top, then some comboboxes. I want the combobox to acquire a list of options from a text file that is described by the previous inputs (file name, the row in which the requisite information is found, delimiter type, etc.). I am trying to use the postcommand, but it seems to run first thing and never update, rather then update every time I access the drop down menu of the combobox.



        datatypes = 
    datatypes = ttk.Combobox(tab_loc, textvariable=std1, values=datatypes, postcommand=self.get_datatypes(datatypes,
    self.flnm2.get(), self.hl2_text.get(), self.delim2.get(), self.fcd2_text.get())).grid(pady=v_pad,
    padx=h_pad, row=8, column=1, sticky=EW)

    def get_datatypes(self, lst, flnm, hl, delim, fcd):
    # Problem: postcommand runs at start of GUI. Prevents updating.
    lst += ["test", "worked?"]
    print("stuff")
    lst += flnm
    lst += hl
    try:
    # open the file, get the line, break it apart.
    except:
    pass


    self.flnm2, self.hl2_text, self.delim2, and self.fcd2_text are some of the previous inputs. They are StringVar.



    When I run this code, the combobox has the options test, worked?, and two blank lines (presumably for flnm and hl). I plan to have multiple comboboxes like this, just with different inputs, so I need a function I can give inputs to and then updates datatypes.



    What am I doing wrong?










    share|improve this question



























      0












      0








      0








      I am creating a GUI using Python 3.4 and Tkinter on a Windows 8 computer.



      The GUI has some Entry inputs at the top, then some comboboxes. I want the combobox to acquire a list of options from a text file that is described by the previous inputs (file name, the row in which the requisite information is found, delimiter type, etc.). I am trying to use the postcommand, but it seems to run first thing and never update, rather then update every time I access the drop down menu of the combobox.



          datatypes = 
      datatypes = ttk.Combobox(tab_loc, textvariable=std1, values=datatypes, postcommand=self.get_datatypes(datatypes,
      self.flnm2.get(), self.hl2_text.get(), self.delim2.get(), self.fcd2_text.get())).grid(pady=v_pad,
      padx=h_pad, row=8, column=1, sticky=EW)

      def get_datatypes(self, lst, flnm, hl, delim, fcd):
      # Problem: postcommand runs at start of GUI. Prevents updating.
      lst += ["test", "worked?"]
      print("stuff")
      lst += flnm
      lst += hl
      try:
      # open the file, get the line, break it apart.
      except:
      pass


      self.flnm2, self.hl2_text, self.delim2, and self.fcd2_text are some of the previous inputs. They are StringVar.



      When I run this code, the combobox has the options test, worked?, and two blank lines (presumably for flnm and hl). I plan to have multiple comboboxes like this, just with different inputs, so I need a function I can give inputs to and then updates datatypes.



      What am I doing wrong?










      share|improve this question
















      I am creating a GUI using Python 3.4 and Tkinter on a Windows 8 computer.



      The GUI has some Entry inputs at the top, then some comboboxes. I want the combobox to acquire a list of options from a text file that is described by the previous inputs (file name, the row in which the requisite information is found, delimiter type, etc.). I am trying to use the postcommand, but it seems to run first thing and never update, rather then update every time I access the drop down menu of the combobox.



          datatypes = 
      datatypes = ttk.Combobox(tab_loc, textvariable=std1, values=datatypes, postcommand=self.get_datatypes(datatypes,
      self.flnm2.get(), self.hl2_text.get(), self.delim2.get(), self.fcd2_text.get())).grid(pady=v_pad,
      padx=h_pad, row=8, column=1, sticky=EW)

      def get_datatypes(self, lst, flnm, hl, delim, fcd):
      # Problem: postcommand runs at start of GUI. Prevents updating.
      lst += ["test", "worked?"]
      print("stuff")
      lst += flnm
      lst += hl
      try:
      # open the file, get the line, break it apart.
      except:
      pass


      self.flnm2, self.hl2_text, self.delim2, and self.fcd2_text are some of the previous inputs. They are StringVar.



      When I run this code, the combobox has the options test, worked?, and two blank lines (presumably for flnm and hl). I plan to have multiple comboboxes like this, just with different inputs, so I need a function I can give inputs to and then updates datatypes.



      What am I doing wrong?







      python-3.x combobox tkinter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 8 '15 at 19:40







      jknicely

















      asked Sep 3 '15 at 21:31









      jknicelyjknicely

      5719




      5719
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You are calling self.get_datatypes(...) and assigning the result to the postcommand attribute at the time you create the combobox. That is why it runs exactly once: you told it to. Just like with command attributes, you must give a reference to a function when definining the postcommand attribute.



          Create a method specifically for the post command for each combobox, use a reference to that for your postcommand, and then call get_datatypes from that function after fetching the values from the other widgets.



          It should look something like this:



          datatypes = ttk.Combobox(..., postcommand=self.combo_post_command, ...)
          ...
          def combo_post_command(self):
          flnm2 = self.flnm2.get()
          hl2_text = self.hl2_text.get()
          delim2 = self.delim2.get()
          fcd2_text = self.fcd2_text.get()
          return self.get_datatypes(datatypes, flnm2, hl2_text, delim2, fcd2_text)


          I'm not exactly sure what datatypes is supposed to be. You define it as an empty list, then reset it to be the widget itself. Regardless, this shows the general concept.



          It may seem like you have a lot of duplicated code by having a function for each combobox, but you have to call all the get() functions somewhere. You either try to cram that all into the configuration of the widget, or you put it in a function. Putting it in the function is more explicit, and easier to debug and maintain over time.






          share|improve this answer































            0














            Actually, you don't need such kind of work. You can finish your homework with a simple lambda function.



            valuetypes=["bla", "bla","bla"]
            datatypes = ttk.Combobox(..., values=valuetypes,
            postcommand=lambda: datatypes.confiure(values=valuetypes), ...)

            valuetypes.append["another bla"]


            When you click on the down-arrow of the Combobox, the changes will appear in the drop-down menu.






            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%2f32385771%2fhow-can-i-dynamically-update-ttk-combobox%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You are calling self.get_datatypes(...) and assigning the result to the postcommand attribute at the time you create the combobox. That is why it runs exactly once: you told it to. Just like with command attributes, you must give a reference to a function when definining the postcommand attribute.



              Create a method specifically for the post command for each combobox, use a reference to that for your postcommand, and then call get_datatypes from that function after fetching the values from the other widgets.



              It should look something like this:



              datatypes = ttk.Combobox(..., postcommand=self.combo_post_command, ...)
              ...
              def combo_post_command(self):
              flnm2 = self.flnm2.get()
              hl2_text = self.hl2_text.get()
              delim2 = self.delim2.get()
              fcd2_text = self.fcd2_text.get()
              return self.get_datatypes(datatypes, flnm2, hl2_text, delim2, fcd2_text)


              I'm not exactly sure what datatypes is supposed to be. You define it as an empty list, then reset it to be the widget itself. Regardless, this shows the general concept.



              It may seem like you have a lot of duplicated code by having a function for each combobox, but you have to call all the get() functions somewhere. You either try to cram that all into the configuration of the widget, or you put it in a function. Putting it in the function is more explicit, and easier to debug and maintain over time.






              share|improve this answer




























                0














                You are calling self.get_datatypes(...) and assigning the result to the postcommand attribute at the time you create the combobox. That is why it runs exactly once: you told it to. Just like with command attributes, you must give a reference to a function when definining the postcommand attribute.



                Create a method specifically for the post command for each combobox, use a reference to that for your postcommand, and then call get_datatypes from that function after fetching the values from the other widgets.



                It should look something like this:



                datatypes = ttk.Combobox(..., postcommand=self.combo_post_command, ...)
                ...
                def combo_post_command(self):
                flnm2 = self.flnm2.get()
                hl2_text = self.hl2_text.get()
                delim2 = self.delim2.get()
                fcd2_text = self.fcd2_text.get()
                return self.get_datatypes(datatypes, flnm2, hl2_text, delim2, fcd2_text)


                I'm not exactly sure what datatypes is supposed to be. You define it as an empty list, then reset it to be the widget itself. Regardless, this shows the general concept.



                It may seem like you have a lot of duplicated code by having a function for each combobox, but you have to call all the get() functions somewhere. You either try to cram that all into the configuration of the widget, or you put it in a function. Putting it in the function is more explicit, and easier to debug and maintain over time.






                share|improve this answer


























                  0












                  0








                  0







                  You are calling self.get_datatypes(...) and assigning the result to the postcommand attribute at the time you create the combobox. That is why it runs exactly once: you told it to. Just like with command attributes, you must give a reference to a function when definining the postcommand attribute.



                  Create a method specifically for the post command for each combobox, use a reference to that for your postcommand, and then call get_datatypes from that function after fetching the values from the other widgets.



                  It should look something like this:



                  datatypes = ttk.Combobox(..., postcommand=self.combo_post_command, ...)
                  ...
                  def combo_post_command(self):
                  flnm2 = self.flnm2.get()
                  hl2_text = self.hl2_text.get()
                  delim2 = self.delim2.get()
                  fcd2_text = self.fcd2_text.get()
                  return self.get_datatypes(datatypes, flnm2, hl2_text, delim2, fcd2_text)


                  I'm not exactly sure what datatypes is supposed to be. You define it as an empty list, then reset it to be the widget itself. Regardless, this shows the general concept.



                  It may seem like you have a lot of duplicated code by having a function for each combobox, but you have to call all the get() functions somewhere. You either try to cram that all into the configuration of the widget, or you put it in a function. Putting it in the function is more explicit, and easier to debug and maintain over time.






                  share|improve this answer













                  You are calling self.get_datatypes(...) and assigning the result to the postcommand attribute at the time you create the combobox. That is why it runs exactly once: you told it to. Just like with command attributes, you must give a reference to a function when definining the postcommand attribute.



                  Create a method specifically for the post command for each combobox, use a reference to that for your postcommand, and then call get_datatypes from that function after fetching the values from the other widgets.



                  It should look something like this:



                  datatypes = ttk.Combobox(..., postcommand=self.combo_post_command, ...)
                  ...
                  def combo_post_command(self):
                  flnm2 = self.flnm2.get()
                  hl2_text = self.hl2_text.get()
                  delim2 = self.delim2.get()
                  fcd2_text = self.fcd2_text.get()
                  return self.get_datatypes(datatypes, flnm2, hl2_text, delim2, fcd2_text)


                  I'm not exactly sure what datatypes is supposed to be. You define it as an empty list, then reset it to be the widget itself. Regardless, this shows the general concept.



                  It may seem like you have a lot of duplicated code by having a function for each combobox, but you have to call all the get() functions somewhere. You either try to cram that all into the configuration of the widget, or you put it in a function. Putting it in the function is more explicit, and easier to debug and maintain over time.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 8 '15 at 21:40









                  Bryan OakleyBryan Oakley

                  216k22258420




                  216k22258420

























                      0














                      Actually, you don't need such kind of work. You can finish your homework with a simple lambda function.



                      valuetypes=["bla", "bla","bla"]
                      datatypes = ttk.Combobox(..., values=valuetypes,
                      postcommand=lambda: datatypes.confiure(values=valuetypes), ...)

                      valuetypes.append["another bla"]


                      When you click on the down-arrow of the Combobox, the changes will appear in the drop-down menu.






                      share|improve this answer




























                        0














                        Actually, you don't need such kind of work. You can finish your homework with a simple lambda function.



                        valuetypes=["bla", "bla","bla"]
                        datatypes = ttk.Combobox(..., values=valuetypes,
                        postcommand=lambda: datatypes.confiure(values=valuetypes), ...)

                        valuetypes.append["another bla"]


                        When you click on the down-arrow of the Combobox, the changes will appear in the drop-down menu.






                        share|improve this answer


























                          0












                          0








                          0







                          Actually, you don't need such kind of work. You can finish your homework with a simple lambda function.



                          valuetypes=["bla", "bla","bla"]
                          datatypes = ttk.Combobox(..., values=valuetypes,
                          postcommand=lambda: datatypes.confiure(values=valuetypes), ...)

                          valuetypes.append["another bla"]


                          When you click on the down-arrow of the Combobox, the changes will appear in the drop-down menu.






                          share|improve this answer













                          Actually, you don't need such kind of work. You can finish your homework with a simple lambda function.



                          valuetypes=["bla", "bla","bla"]
                          datatypes = ttk.Combobox(..., values=valuetypes,
                          postcommand=lambda: datatypes.confiure(values=valuetypes), ...)

                          valuetypes.append["another bla"]


                          When you click on the down-arrow of the Combobox, the changes will appear in the drop-down menu.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 30 '18 at 14:50









                          Fatih1923Fatih1923

                          829915




                          829915






























                              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%2f32385771%2fhow-can-i-dynamically-update-ttk-combobox%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

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas