How to make each radiobutton unique from the others in tkinter












0















I have a window in Tkinter that looks like this:
enter image description here



When i click on a button in the first row, it stays. However, when i click on a button in the second row, it unselects the one i chose above.



I want it to be able to only select one option per row. Is there something I'm missing? When it's done, I want to be able to iterate over the rows and get the value of the boxes, but I'm not sure how to do that either.
The code for that section is:



for i in studentList:
Label(left,text=i[0][::]+' ' + i[1][::],fg='black',bg='#dbdbdb',font=('Arial',11,'bold')).grid(row=counter,column=0,pady=13,sticky='news')
P = Radiobutton(right,text='Present',bg='#56ab32',fg='black',value='P'+str(counter),indicatoron = 0,font=('Arial',12,'bold'))
P.grid(row=counter,column=0,pady=10,padx=20,sticky='news')
L = Radiobutton(right,text='Leave',bg='#e6a800',fg='white',indicatoron = 0,value='L'+str(counter),font=('Arial',12,'bold'))
L.grid(row=counter,column=1,pady=10,padx=20,sticky='news')
Radiobutton(right,text='Absent',bg='#bd2900',fg='white',indicatoron = 0,value='A'+str(counter),font=('Arial',12,'bold')).grid(row=counter,column=2,pady=10,padx=20,sticky='news')
counter+=1









share|improve this question





























    0















    I have a window in Tkinter that looks like this:
    enter image description here



    When i click on a button in the first row, it stays. However, when i click on a button in the second row, it unselects the one i chose above.



    I want it to be able to only select one option per row. Is there something I'm missing? When it's done, I want to be able to iterate over the rows and get the value of the boxes, but I'm not sure how to do that either.
    The code for that section is:



    for i in studentList:
    Label(left,text=i[0][::]+' ' + i[1][::],fg='black',bg='#dbdbdb',font=('Arial',11,'bold')).grid(row=counter,column=0,pady=13,sticky='news')
    P = Radiobutton(right,text='Present',bg='#56ab32',fg='black',value='P'+str(counter),indicatoron = 0,font=('Arial',12,'bold'))
    P.grid(row=counter,column=0,pady=10,padx=20,sticky='news')
    L = Radiobutton(right,text='Leave',bg='#e6a800',fg='white',indicatoron = 0,value='L'+str(counter),font=('Arial',12,'bold'))
    L.grid(row=counter,column=1,pady=10,padx=20,sticky='news')
    Radiobutton(right,text='Absent',bg='#bd2900',fg='white',indicatoron = 0,value='A'+str(counter),font=('Arial',12,'bold')).grid(row=counter,column=2,pady=10,padx=20,sticky='news')
    counter+=1









    share|improve this question



























      0












      0








      0








      I have a window in Tkinter that looks like this:
      enter image description here



      When i click on a button in the first row, it stays. However, when i click on a button in the second row, it unselects the one i chose above.



      I want it to be able to only select one option per row. Is there something I'm missing? When it's done, I want to be able to iterate over the rows and get the value of the boxes, but I'm not sure how to do that either.
      The code for that section is:



      for i in studentList:
      Label(left,text=i[0][::]+' ' + i[1][::],fg='black',bg='#dbdbdb',font=('Arial',11,'bold')).grid(row=counter,column=0,pady=13,sticky='news')
      P = Radiobutton(right,text='Present',bg='#56ab32',fg='black',value='P'+str(counter),indicatoron = 0,font=('Arial',12,'bold'))
      P.grid(row=counter,column=0,pady=10,padx=20,sticky='news')
      L = Radiobutton(right,text='Leave',bg='#e6a800',fg='white',indicatoron = 0,value='L'+str(counter),font=('Arial',12,'bold'))
      L.grid(row=counter,column=1,pady=10,padx=20,sticky='news')
      Radiobutton(right,text='Absent',bg='#bd2900',fg='white',indicatoron = 0,value='A'+str(counter),font=('Arial',12,'bold')).grid(row=counter,column=2,pady=10,padx=20,sticky='news')
      counter+=1









      share|improve this question
















      I have a window in Tkinter that looks like this:
      enter image description here



      When i click on a button in the first row, it stays. However, when i click on a button in the second row, it unselects the one i chose above.



      I want it to be able to only select one option per row. Is there something I'm missing? When it's done, I want to be able to iterate over the rows and get the value of the boxes, but I'm not sure how to do that either.
      The code for that section is:



      for i in studentList:
      Label(left,text=i[0][::]+' ' + i[1][::],fg='black',bg='#dbdbdb',font=('Arial',11,'bold')).grid(row=counter,column=0,pady=13,sticky='news')
      P = Radiobutton(right,text='Present',bg='#56ab32',fg='black',value='P'+str(counter),indicatoron = 0,font=('Arial',12,'bold'))
      P.grid(row=counter,column=0,pady=10,padx=20,sticky='news')
      L = Radiobutton(right,text='Leave',bg='#e6a800',fg='white',indicatoron = 0,value='L'+str(counter),font=('Arial',12,'bold'))
      L.grid(row=counter,column=1,pady=10,padx=20,sticky='news')
      Radiobutton(right,text='Absent',bg='#bd2900',fg='white',indicatoron = 0,value='A'+str(counter),font=('Arial',12,'bold')).grid(row=counter,column=2,pady=10,padx=20,sticky='news')
      counter+=1






      python python-3.x tkinter radio-button






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 16:04









      Miraj50

      2,78011025




      2,78011025










      asked Jan 3 at 14:40









      Osasenaga EmokpaeOsasenaga Emokpae

      143




      143
























          1 Answer
          1






          active

          oldest

          votes


















          -1














          Radiobuttons work by assigning two or more radiobuttons the same instance of one of tkinter's special variable objects -- usuallyStringVar or IntVar. This sharing of a variable is what makes a group of radiobuttons work as a set, since the variable can only hold a single value.



          Because you aren't assigning a variable, tkinter is using a default variable which is the same for every button. Thus, all buttons are acting as a single set.



          To make your code work, each row needs to use it's own instance of StringVar. It would look something like this:



          vars = 
          for i in studentList:
          var = StringVar()
          vars.append(var)
          ...
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          ...


          With the above, you can get the choice of each row by getting the value of the variable for that row. For example, the first row would be vars[0].get(), the second row would be vars[1].get() and so on.






          share|improve this answer
























          • I can't begin to understand why this has a downvote. This is unquestionably the correct solution. <shrug>

            – Bryan Oakley
            Jan 3 at 18:51












          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%2f54024461%2fhow-to-make-each-radiobutton-unique-from-the-others-in-tkinter%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














          Radiobuttons work by assigning two or more radiobuttons the same instance of one of tkinter's special variable objects -- usuallyStringVar or IntVar. This sharing of a variable is what makes a group of radiobuttons work as a set, since the variable can only hold a single value.



          Because you aren't assigning a variable, tkinter is using a default variable which is the same for every button. Thus, all buttons are acting as a single set.



          To make your code work, each row needs to use it's own instance of StringVar. It would look something like this:



          vars = 
          for i in studentList:
          var = StringVar()
          vars.append(var)
          ...
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          ...


          With the above, you can get the choice of each row by getting the value of the variable for that row. For example, the first row would be vars[0].get(), the second row would be vars[1].get() and so on.






          share|improve this answer
























          • I can't begin to understand why this has a downvote. This is unquestionably the correct solution. <shrug>

            – Bryan Oakley
            Jan 3 at 18:51
















          -1














          Radiobuttons work by assigning two or more radiobuttons the same instance of one of tkinter's special variable objects -- usuallyStringVar or IntVar. This sharing of a variable is what makes a group of radiobuttons work as a set, since the variable can only hold a single value.



          Because you aren't assigning a variable, tkinter is using a default variable which is the same for every button. Thus, all buttons are acting as a single set.



          To make your code work, each row needs to use it's own instance of StringVar. It would look something like this:



          vars = 
          for i in studentList:
          var = StringVar()
          vars.append(var)
          ...
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          ...


          With the above, you can get the choice of each row by getting the value of the variable for that row. For example, the first row would be vars[0].get(), the second row would be vars[1].get() and so on.






          share|improve this answer
























          • I can't begin to understand why this has a downvote. This is unquestionably the correct solution. <shrug>

            – Bryan Oakley
            Jan 3 at 18:51














          -1












          -1








          -1







          Radiobuttons work by assigning two or more radiobuttons the same instance of one of tkinter's special variable objects -- usuallyStringVar or IntVar. This sharing of a variable is what makes a group of radiobuttons work as a set, since the variable can only hold a single value.



          Because you aren't assigning a variable, tkinter is using a default variable which is the same for every button. Thus, all buttons are acting as a single set.



          To make your code work, each row needs to use it's own instance of StringVar. It would look something like this:



          vars = 
          for i in studentList:
          var = StringVar()
          vars.append(var)
          ...
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          ...


          With the above, you can get the choice of each row by getting the value of the variable for that row. For example, the first row would be vars[0].get(), the second row would be vars[1].get() and so on.






          share|improve this answer













          Radiobuttons work by assigning two or more radiobuttons the same instance of one of tkinter's special variable objects -- usuallyStringVar or IntVar. This sharing of a variable is what makes a group of radiobuttons work as a set, since the variable can only hold a single value.



          Because you aren't assigning a variable, tkinter is using a default variable which is the same for every button. Thus, all buttons are acting as a single set.



          To make your code work, each row needs to use it's own instance of StringVar. It would look something like this:



          vars = 
          for i in studentList:
          var = StringVar()
          vars.append(var)
          ...
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          Radiobutton(right, variable=var, ...)
          ...


          With the above, you can get the choice of each row by getting the value of the variable for that row. For example, the first row would be vars[0].get(), the second row would be vars[1].get() and so on.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 15:51









          Bryan OakleyBryan Oakley

          221k22276434




          221k22276434













          • I can't begin to understand why this has a downvote. This is unquestionably the correct solution. <shrug>

            – Bryan Oakley
            Jan 3 at 18:51



















          • I can't begin to understand why this has a downvote. This is unquestionably the correct solution. <shrug>

            – Bryan Oakley
            Jan 3 at 18:51

















          I can't begin to understand why this has a downvote. This is unquestionably the correct solution. <shrug>

          – Bryan Oakley
          Jan 3 at 18:51





          I can't begin to understand why this has a downvote. This is unquestionably the correct solution. <shrug>

          – Bryan Oakley
          Jan 3 at 18:51




















          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%2f54024461%2fhow-to-make-each-radiobutton-unique-from-the-others-in-tkinter%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