Change buttons relief on click using tkinter












1















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



























    1















    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

























      1












      1








      1








      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 Nov 20 '18 at 21:10









      BahlsenBahlsen

      215




      215
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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
            Nov 21 '18 at 9:46











          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%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









          0














          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
            Nov 21 '18 at 9:46
















          0














          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
            Nov 21 '18 at 9:46














          0












          0








          0







          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 Nov 20 '18 at 21:44









          Miraj50Miraj50

          2,6451824




          2,6451824













          • 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
            Nov 21 '18 at 9:46



















          • 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
            Nov 21 '18 at 9:46

















          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
          Nov 21 '18 at 9:46





          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
          Nov 21 '18 at 9:46


















          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%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy