Change buttons relief on click using tkinter











up vote
1
down vote

favorite












I want to create buttons labelled with names from a list. When you click on a button its relief shall change from groove to sunken. There is one condition, only one button is allowed to be sunken. Thus, when you click on a button while another one is already sunken, the sunken one has to go back to groove.
How it looks like



I was able to put my idea into action and coded the whole thing. However, I'm wondering if there might be a better way to implement it. What is your opinion? Here is my code:



import tkinter as tk
from functools import partial

class ButtonSunken:
def __init__(self):
self.tags = ('A','B','C','D','E','F')
self.buttons =
self.win = tk.Tk()
self.create_buttons()
self.win.mainloop()

def create_buttons(self):
for j,i in enumerate(self.tags):
self.buttons.append(tk.Button(self.win, text = i))
self.buttons[-1].grid(column=0, row=j)
ho_general = partial(self.button_pressed, self.buttons[-1])
self.buttons[-1].configure(command = ho_general)

def button_pressed(self, button):
try: # first time active_button does not exist yet
self.active_button.configure(relief = 'groove')
except:
pass
button.configure(relief = 'sunken')
self.active_button = button

t_object = ButtonSunken()


Thank you very much for your help!










share|improve this question


























    up vote
    1
    down vote

    favorite












    I want to create buttons labelled with names from a list. When you click on a button its relief shall change from groove to sunken. There is one condition, only one button is allowed to be sunken. Thus, when you click on a button while another one is already sunken, the sunken one has to go back to groove.
    How it looks like



    I was able to put my idea into action and coded the whole thing. However, I'm wondering if there might be a better way to implement it. What is your opinion? Here is my code:



    import tkinter as tk
    from functools import partial

    class ButtonSunken:
    def __init__(self):
    self.tags = ('A','B','C','D','E','F')
    self.buttons =
    self.win = tk.Tk()
    self.create_buttons()
    self.win.mainloop()

    def create_buttons(self):
    for j,i in enumerate(self.tags):
    self.buttons.append(tk.Button(self.win, text = i))
    self.buttons[-1].grid(column=0, row=j)
    ho_general = partial(self.button_pressed, self.buttons[-1])
    self.buttons[-1].configure(command = ho_general)

    def button_pressed(self, button):
    try: # first time active_button does not exist yet
    self.active_button.configure(relief = 'groove')
    except:
    pass
    button.configure(relief = 'sunken')
    self.active_button = button

    t_object = ButtonSunken()


    Thank you very much for your help!










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want to create buttons labelled with names from a list. When you click on a button its relief shall change from groove to sunken. There is one condition, only one button is allowed to be sunken. Thus, when you click on a button while another one is already sunken, the sunken one has to go back to groove.
      How it looks like



      I was able to put my idea into action and coded the whole thing. However, I'm wondering if there might be a better way to implement it. What is your opinion? Here is my code:



      import tkinter as tk
      from functools import partial

      class ButtonSunken:
      def __init__(self):
      self.tags = ('A','B','C','D','E','F')
      self.buttons =
      self.win = tk.Tk()
      self.create_buttons()
      self.win.mainloop()

      def create_buttons(self):
      for j,i in enumerate(self.tags):
      self.buttons.append(tk.Button(self.win, text = i))
      self.buttons[-1].grid(column=0, row=j)
      ho_general = partial(self.button_pressed, self.buttons[-1])
      self.buttons[-1].configure(command = ho_general)

      def button_pressed(self, button):
      try: # first time active_button does not exist yet
      self.active_button.configure(relief = 'groove')
      except:
      pass
      button.configure(relief = 'sunken')
      self.active_button = button

      t_object = ButtonSunken()


      Thank you very much for your help!










      share|improve this question













      I want to create buttons labelled with names from a list. When you click on a button its relief shall change from groove to sunken. There is one condition, only one button is allowed to be sunken. Thus, when you click on a button while another one is already sunken, the sunken one has to go back to groove.
      How it looks like



      I was able to put my idea into action and coded the whole thing. However, I'm wondering if there might be a better way to implement it. What is your opinion? Here is my code:



      import tkinter as tk
      from functools import partial

      class ButtonSunken:
      def __init__(self):
      self.tags = ('A','B','C','D','E','F')
      self.buttons =
      self.win = tk.Tk()
      self.create_buttons()
      self.win.mainloop()

      def create_buttons(self):
      for j,i in enumerate(self.tags):
      self.buttons.append(tk.Button(self.win, text = i))
      self.buttons[-1].grid(column=0, row=j)
      ho_general = partial(self.button_pressed, self.buttons[-1])
      self.buttons[-1].configure(command = ho_general)

      def button_pressed(self, button):
      try: # first time active_button does not exist yet
      self.active_button.configure(relief = 'groove')
      except:
      pass
      button.configure(relief = 'sunken')
      self.active_button = button

      t_object = ButtonSunken()


      Thank you very much for your help!







      python button tkinter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Bahlsen

      114




      114
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Your method is pretty much good, just that it can be done without using any special functions. In my code, I just store the index of the current active button and set its relief to groove whenever the next button is pressed whose relief is in turn changed to sunken. Have a look at the code.



          import tkinter as tk

          class ButtonSunken:
          def __init__(self):
          self.tags = ('A','B','C','D','E','F')
          self.buttons =
          self.active = None
          self.win = tk.Tk()
          self.create_buttons()
          self.win.mainloop()

          def create_buttons(self):
          for j,i in enumerate(self.tags):
          self.buttons.append(tk.Button(self.win, text=i, command=lambda x=j: self.button_pressed(x)))
          self.buttons[-1].grid(column=0, row=j)

          def button_pressed(self, idx):
          if self.active is not None:
          self.buttons[self.active].configure(relief='groove')
          self.buttons[idx].configure(relief='sunken')
          self.active = idx

          t_object = ButtonSunken()





          share|improve this answer





















          • Thanks for your reply. I like your solution, it's much better/easier to hand over the index than the whole object. Up to now, I was a bit reluctant towards the Lambda function, however, I realize it's pretty useful.
            – Bahlsen
            14 hours ago











          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',
          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%2f53401582%2fchange-buttons-relief-on-click-using-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








          up vote
          0
          down vote



          accepted










          Your method is pretty much good, just that it can be done without using any special functions. In my code, I just store the index of the current active button and set its relief to groove whenever the next button is pressed whose relief is in turn changed to sunken. Have a look at the code.



          import tkinter as tk

          class ButtonSunken:
          def __init__(self):
          self.tags = ('A','B','C','D','E','F')
          self.buttons =
          self.active = None
          self.win = tk.Tk()
          self.create_buttons()
          self.win.mainloop()

          def create_buttons(self):
          for j,i in enumerate(self.tags):
          self.buttons.append(tk.Button(self.win, text=i, command=lambda x=j: self.button_pressed(x)))
          self.buttons[-1].grid(column=0, row=j)

          def button_pressed(self, idx):
          if self.active is not None:
          self.buttons[self.active].configure(relief='groove')
          self.buttons[idx].configure(relief='sunken')
          self.active = idx

          t_object = ButtonSunken()





          share|improve this answer





















          • Thanks for your reply. I like your solution, it's much better/easier to hand over the index than the whole object. Up to now, I was a bit reluctant towards the Lambda function, however, I realize it's pretty useful.
            – Bahlsen
            14 hours ago















          up vote
          0
          down vote



          accepted










          Your method is pretty much good, just that it can be done without using any special functions. In my code, I just store the index of the current active button and set its relief to groove whenever the next button is pressed whose relief is in turn changed to sunken. Have a look at the code.



          import tkinter as tk

          class ButtonSunken:
          def __init__(self):
          self.tags = ('A','B','C','D','E','F')
          self.buttons =
          self.active = None
          self.win = tk.Tk()
          self.create_buttons()
          self.win.mainloop()

          def create_buttons(self):
          for j,i in enumerate(self.tags):
          self.buttons.append(tk.Button(self.win, text=i, command=lambda x=j: self.button_pressed(x)))
          self.buttons[-1].grid(column=0, row=j)

          def button_pressed(self, idx):
          if self.active is not None:
          self.buttons[self.active].configure(relief='groove')
          self.buttons[idx].configure(relief='sunken')
          self.active = idx

          t_object = ButtonSunken()





          share|improve this answer





















          • Thanks for your reply. I like your solution, it's much better/easier to hand over the index than the whole object. Up to now, I was a bit reluctant towards the Lambda function, however, I realize it's pretty useful.
            – Bahlsen
            14 hours ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Your method is pretty much good, just that it can be done without using any special functions. In my code, I just store the index of the current active button and set its relief to groove whenever the next button is pressed whose relief is in turn changed to sunken. Have a look at the code.



          import tkinter as tk

          class ButtonSunken:
          def __init__(self):
          self.tags = ('A','B','C','D','E','F')
          self.buttons =
          self.active = None
          self.win = tk.Tk()
          self.create_buttons()
          self.win.mainloop()

          def create_buttons(self):
          for j,i in enumerate(self.tags):
          self.buttons.append(tk.Button(self.win, text=i, command=lambda x=j: self.button_pressed(x)))
          self.buttons[-1].grid(column=0, row=j)

          def button_pressed(self, idx):
          if self.active is not None:
          self.buttons[self.active].configure(relief='groove')
          self.buttons[idx].configure(relief='sunken')
          self.active = idx

          t_object = ButtonSunken()





          share|improve this answer












          Your method is pretty much good, just that it can be done without using any special functions. In my code, I just store the index of the current active button and set its relief to groove whenever the next button is pressed whose relief is in turn changed to sunken. Have a look at the code.



          import tkinter as tk

          class ButtonSunken:
          def __init__(self):
          self.tags = ('A','B','C','D','E','F')
          self.buttons =
          self.active = None
          self.win = tk.Tk()
          self.create_buttons()
          self.win.mainloop()

          def create_buttons(self):
          for j,i in enumerate(self.tags):
          self.buttons.append(tk.Button(self.win, text=i, command=lambda x=j: self.button_pressed(x)))
          self.buttons[-1].grid(column=0, row=j)

          def button_pressed(self, idx):
          if self.active is not None:
          self.buttons[self.active].configure(relief='groove')
          self.buttons[idx].configure(relief='sunken')
          self.active = idx

          t_object = ButtonSunken()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Miraj50

          1,513517




          1,513517












          • Thanks for your reply. I like your solution, it's much better/easier to hand over the index than the whole object. Up to now, I was a bit reluctant towards the Lambda function, however, I realize it's pretty useful.
            – Bahlsen
            14 hours ago


















          • Thanks for your reply. I like your solution, it's much better/easier to hand over the index than the whole object. Up to now, I was a bit reluctant towards the Lambda function, however, I realize it's pretty useful.
            – Bahlsen
            14 hours ago
















          Thanks for your reply. I like your solution, it's much better/easier to hand over the index than the whole object. Up to now, I was a bit reluctant towards the Lambda function, however, I realize it's pretty useful.
          – Bahlsen
          14 hours ago




          Thanks for your reply. I like your solution, it's much better/easier to hand over the index than the whole object. Up to now, I was a bit reluctant towards the Lambda function, however, I realize it's pretty useful.
          – Bahlsen
          14 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401582%2fchange-buttons-relief-on-click-using-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

          Sphinx de Gizeh

          Dijon

          Guerrita