Tkinter Optionmenu StringVar.get() returning blank










1















I am creating a Tkinter window with a 'for loop' so it can self-adjust if later on, I decide to add more questions. My issue is that I can't save the inputted value on the optionmenu. So far all I got was a list1 = ['', '', ''] while the Strg_var = [StringVar, StringVar, StringVar] and it prints only the blanks and the variables PY_numbers.



import tkinter as tk
from tkinter import *

LARGE_FONT = ("Arial", 12)

window=Tk()

def _save():
print(*list1, sep = ", ")
print(*Strg_var, sep = ", ")

Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
choices = ['-', 'Yes', 'No']
n = 0
Strg_var=[0]*len(Questionlist)
list1=
for n in range(len(Questionlist)):
Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
a = tk.StringVar(window)
OptionMenu(window, a, choices[0], *choices).grid(row = n, column=2, padx=10, sticky="WE")
list1.append(a.get())

tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

window.mainloop()


Can someone help me to sort this out on how to save the optionmenu user selections into a list or any other way?










share|improve this question




























    1















    I am creating a Tkinter window with a 'for loop' so it can self-adjust if later on, I decide to add more questions. My issue is that I can't save the inputted value on the optionmenu. So far all I got was a list1 = ['', '', ''] while the Strg_var = [StringVar, StringVar, StringVar] and it prints only the blanks and the variables PY_numbers.



    import tkinter as tk
    from tkinter import *

    LARGE_FONT = ("Arial", 12)

    window=Tk()

    def _save():
    print(*list1, sep = ", ")
    print(*Strg_var, sep = ", ")

    Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
    choices = ['-', 'Yes', 'No']
    n = 0
    Strg_var=[0]*len(Questionlist)
    list1=
    for n in range(len(Questionlist)):
    Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
    a = tk.StringVar(window)
    OptionMenu(window, a, choices[0], *choices).grid(row = n, column=2, padx=10, sticky="WE")
    list1.append(a.get())

    tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

    window.mainloop()


    Can someone help me to sort this out on how to save the optionmenu user selections into a list or any other way?










    share|improve this question


























      1












      1








      1








      I am creating a Tkinter window with a 'for loop' so it can self-adjust if later on, I decide to add more questions. My issue is that I can't save the inputted value on the optionmenu. So far all I got was a list1 = ['', '', ''] while the Strg_var = [StringVar, StringVar, StringVar] and it prints only the blanks and the variables PY_numbers.



      import tkinter as tk
      from tkinter import *

      LARGE_FONT = ("Arial", 12)

      window=Tk()

      def _save():
      print(*list1, sep = ", ")
      print(*Strg_var, sep = ", ")

      Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
      choices = ['-', 'Yes', 'No']
      n = 0
      Strg_var=[0]*len(Questionlist)
      list1=
      for n in range(len(Questionlist)):
      Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
      a = tk.StringVar(window)
      OptionMenu(window, a, choices[0], *choices).grid(row = n, column=2, padx=10, sticky="WE")
      list1.append(a.get())

      tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

      window.mainloop()


      Can someone help me to sort this out on how to save the optionmenu user selections into a list or any other way?










      share|improve this question
















      I am creating a Tkinter window with a 'for loop' so it can self-adjust if later on, I decide to add more questions. My issue is that I can't save the inputted value on the optionmenu. So far all I got was a list1 = ['', '', ''] while the Strg_var = [StringVar, StringVar, StringVar] and it prints only the blanks and the variables PY_numbers.



      import tkinter as tk
      from tkinter import *

      LARGE_FONT = ("Arial", 12)

      window=Tk()

      def _save():
      print(*list1, sep = ", ")
      print(*Strg_var, sep = ", ")

      Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
      choices = ['-', 'Yes', 'No']
      n = 0
      Strg_var=[0]*len(Questionlist)
      list1=
      for n in range(len(Questionlist)):
      Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
      a = tk.StringVar(window)
      OptionMenu(window, a, choices[0], *choices).grid(row = n, column=2, padx=10, sticky="WE")
      list1.append(a.get())

      tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

      window.mainloop()


      Can someone help me to sort this out on how to save the optionmenu user selections into a list or any other way?







      python tkinter optionmenu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 16:58









      Miraj50

      2,7351824




      2,7351824










      asked Nov 13 '18 at 14:57









      Rafael Castelo BrancoRafael Castelo Branco

      395




      395






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can make a list of StringVar(Do Initialise them, I haven't done that in my code). Every time an option is selected the corresponding item will change. So I would do it like this.



          import tkinter as tk

          LARGE_FONT = ("Arial", 12)

          window=tk.Tk()

          def _save():
          print(list(map(lambda x: x.get(), a)))

          Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
          choices = ['-', 'Yes', 'No']
          a = [tk.StringVar(window) for i in range(len(Questionlist))]
          n = 0
          for n in range(len(Questionlist)):
          tk.Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
          Strg_var = tk.StringVar(window)
          tk.OptionMenu(window, a[n], *choices).grid(row = n, column=2, padx=10, sticky="WE")

          tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

          window.mainloop()


          enter image description here



          Output:



          ['No', '-', 'Yes']






          share|improve this answer

























          • One issue that appeared is that if user decides to change its answer the list won't understand that, so I can't manipulate the end result, which is one of the objectives of the program. Do you have any idea on how to solve that?

            – Rafael Castelo Branco
            Nov 13 '18 at 16:01











          • @RafaelCasteloBranco Edited my answer. See if it fits your description.

            – Miraj50
            Nov 13 '18 at 16:35











          • Thanks! it worked with a small adjustment as the list becomes a local variable and I can't work with it, but setting it as a global variable 'z' makes the charm work def _save(): global z z= list(map(lambda x: x.get(), a)) print(z)

            – Rafael Castelo Branco
            Nov 13 '18 at 17:01











          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%2f53283766%2ftkinter-optionmenu-stringvar-get-returning-blank%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can make a list of StringVar(Do Initialise them, I haven't done that in my code). Every time an option is selected the corresponding item will change. So I would do it like this.



          import tkinter as tk

          LARGE_FONT = ("Arial", 12)

          window=tk.Tk()

          def _save():
          print(list(map(lambda x: x.get(), a)))

          Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
          choices = ['-', 'Yes', 'No']
          a = [tk.StringVar(window) for i in range(len(Questionlist))]
          n = 0
          for n in range(len(Questionlist)):
          tk.Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
          Strg_var = tk.StringVar(window)
          tk.OptionMenu(window, a[n], *choices).grid(row = n, column=2, padx=10, sticky="WE")

          tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

          window.mainloop()


          enter image description here



          Output:



          ['No', '-', 'Yes']






          share|improve this answer

























          • One issue that appeared is that if user decides to change its answer the list won't understand that, so I can't manipulate the end result, which is one of the objectives of the program. Do you have any idea on how to solve that?

            – Rafael Castelo Branco
            Nov 13 '18 at 16:01











          • @RafaelCasteloBranco Edited my answer. See if it fits your description.

            – Miraj50
            Nov 13 '18 at 16:35











          • Thanks! it worked with a small adjustment as the list becomes a local variable and I can't work with it, but setting it as a global variable 'z' makes the charm work def _save(): global z z= list(map(lambda x: x.get(), a)) print(z)

            – Rafael Castelo Branco
            Nov 13 '18 at 17:01
















          1














          You can make a list of StringVar(Do Initialise them, I haven't done that in my code). Every time an option is selected the corresponding item will change. So I would do it like this.



          import tkinter as tk

          LARGE_FONT = ("Arial", 12)

          window=tk.Tk()

          def _save():
          print(list(map(lambda x: x.get(), a)))

          Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
          choices = ['-', 'Yes', 'No']
          a = [tk.StringVar(window) for i in range(len(Questionlist))]
          n = 0
          for n in range(len(Questionlist)):
          tk.Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
          Strg_var = tk.StringVar(window)
          tk.OptionMenu(window, a[n], *choices).grid(row = n, column=2, padx=10, sticky="WE")

          tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

          window.mainloop()


          enter image description here



          Output:



          ['No', '-', 'Yes']






          share|improve this answer

























          • One issue that appeared is that if user decides to change its answer the list won't understand that, so I can't manipulate the end result, which is one of the objectives of the program. Do you have any idea on how to solve that?

            – Rafael Castelo Branco
            Nov 13 '18 at 16:01











          • @RafaelCasteloBranco Edited my answer. See if it fits your description.

            – Miraj50
            Nov 13 '18 at 16:35











          • Thanks! it worked with a small adjustment as the list becomes a local variable and I can't work with it, but setting it as a global variable 'z' makes the charm work def _save(): global z z= list(map(lambda x: x.get(), a)) print(z)

            – Rafael Castelo Branco
            Nov 13 '18 at 17:01














          1












          1








          1







          You can make a list of StringVar(Do Initialise them, I haven't done that in my code). Every time an option is selected the corresponding item will change. So I would do it like this.



          import tkinter as tk

          LARGE_FONT = ("Arial", 12)

          window=tk.Tk()

          def _save():
          print(list(map(lambda x: x.get(), a)))

          Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
          choices = ['-', 'Yes', 'No']
          a = [tk.StringVar(window) for i in range(len(Questionlist))]
          n = 0
          for n in range(len(Questionlist)):
          tk.Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
          Strg_var = tk.StringVar(window)
          tk.OptionMenu(window, a[n], *choices).grid(row = n, column=2, padx=10, sticky="WE")

          tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

          window.mainloop()


          enter image description here



          Output:



          ['No', '-', 'Yes']






          share|improve this answer















          You can make a list of StringVar(Do Initialise them, I haven't done that in my code). Every time an option is selected the corresponding item will change. So I would do it like this.



          import tkinter as tk

          LARGE_FONT = ("Arial", 12)

          window=tk.Tk()

          def _save():
          print(list(map(lambda x: x.get(), a)))

          Questionlist = ["A. Is A true? :", "B. Is B true? :", "C. Is C true? :"]
          choices = ['-', 'Yes', 'No']
          a = [tk.StringVar(window) for i in range(len(Questionlist))]
          n = 0
          for n in range(len(Questionlist)):
          tk.Label(window, text=Questionlist[n], font = LARGE_FONT, background = "white").grid(row= n, column=0, columnspan=2, padx=10, pady = 10, sticky="W")
          Strg_var = tk.StringVar(window)
          tk.OptionMenu(window, a[n], *choices).grid(row = n, column=2, padx=10, sticky="WE")

          tk.Button(window, text="Save", command = _save,width=18).grid(row=16, column=0, padx=10, pady=15, sticky="W")

          window.mainloop()


          enter image description here



          Output:



          ['No', '-', 'Yes']







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 16:34

























          answered Nov 13 '18 at 15:27









          Miraj50Miraj50

          2,7351824




          2,7351824












          • One issue that appeared is that if user decides to change its answer the list won't understand that, so I can't manipulate the end result, which is one of the objectives of the program. Do you have any idea on how to solve that?

            – Rafael Castelo Branco
            Nov 13 '18 at 16:01











          • @RafaelCasteloBranco Edited my answer. See if it fits your description.

            – Miraj50
            Nov 13 '18 at 16:35











          • Thanks! it worked with a small adjustment as the list becomes a local variable and I can't work with it, but setting it as a global variable 'z' makes the charm work def _save(): global z z= list(map(lambda x: x.get(), a)) print(z)

            – Rafael Castelo Branco
            Nov 13 '18 at 17:01


















          • One issue that appeared is that if user decides to change its answer the list won't understand that, so I can't manipulate the end result, which is one of the objectives of the program. Do you have any idea on how to solve that?

            – Rafael Castelo Branco
            Nov 13 '18 at 16:01











          • @RafaelCasteloBranco Edited my answer. See if it fits your description.

            – Miraj50
            Nov 13 '18 at 16:35











          • Thanks! it worked with a small adjustment as the list becomes a local variable and I can't work with it, but setting it as a global variable 'z' makes the charm work def _save(): global z z= list(map(lambda x: x.get(), a)) print(z)

            – Rafael Castelo Branco
            Nov 13 '18 at 17:01

















          One issue that appeared is that if user decides to change its answer the list won't understand that, so I can't manipulate the end result, which is one of the objectives of the program. Do you have any idea on how to solve that?

          – Rafael Castelo Branco
          Nov 13 '18 at 16:01





          One issue that appeared is that if user decides to change its answer the list won't understand that, so I can't manipulate the end result, which is one of the objectives of the program. Do you have any idea on how to solve that?

          – Rafael Castelo Branco
          Nov 13 '18 at 16:01













          @RafaelCasteloBranco Edited my answer. See if it fits your description.

          – Miraj50
          Nov 13 '18 at 16:35





          @RafaelCasteloBranco Edited my answer. See if it fits your description.

          – Miraj50
          Nov 13 '18 at 16:35













          Thanks! it worked with a small adjustment as the list becomes a local variable and I can't work with it, but setting it as a global variable 'z' makes the charm work def _save(): global z z= list(map(lambda x: x.get(), a)) print(z)

          – Rafael Castelo Branco
          Nov 13 '18 at 17:01






          Thanks! it worked with a small adjustment as the list becomes a local variable and I can't work with it, but setting it as a global variable 'z' makes the charm work def _save(): global z z= list(map(lambda x: x.get(), a)) print(z)

          – Rafael Castelo Branco
          Nov 13 '18 at 17:01


















          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%2f53283766%2ftkinter-optionmenu-stringvar-get-returning-blank%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto