Updating Button's background_color not reflected on the UI sometimes










0















Well, I'm creating a Kivy app in which you can select only one of many statuses. Every status has his own Button and the selected status has a different background_color from the others.



The problem is that sometimes (apparently random) after clicking a button, two of them stays with his background changed at the same time. The strange thing is that I'm checking the background_color of those elements and it doesn't match with the result that I'm seeing on the screen.



So, the background_color property has one color but another one is being rendered on the screen.



Relevant kv file section:



<StatusButtonsContainer>:
cols: 2
spacing: 8
padding: 0,16,0,0

<StatusButton>:
selected: False
text: self.status_name
on_release: app.on_change_status_click(self.status_name)
font_size: '16'
background_color: self.back_color if self.selected else (0.259, 0.259, 0.259,1)
background_normal: ''
background_down: ''
background_disabled_normal: ''


This is how I'm creating the Button widgets dinamically:



class StatusButtonsContainer(GridLayout):

def __init__(self, **kwargs):
super(StatusButtonsContainer, self).__init__(**kwargs)
for name, color in config.statuses.items():
button = StatusButton(status_name=name, back_color=color)
self.add_widget(button)


class StatusButton(Button):
status_name = StringProperty()
back_color = ListProperty()


And this the function that is executed when the button is pressed:



class ControlsScreen(Screen):

def change_selected_status(self, status):
for button in self.ids.buttons_container.children:
if button.status_name == status:
button.selected = True
button.disabled = True
print('Status ' + button.status_name + ' was selected.')
print('background_color:' + str(button.background_color))
else:
button.selected = False
button.disabled = status in ['printing', 'preparing', 'paused']
print('Status ' + button.status_name + ' was NOT selected.')
print('background_color:' + str(button.background_color))


It's even more weird that this is happening on a Raspberry Pi 3 with Raspbian, but I'm not able to reproduce it on a Windows machine... I double checked that the [input] section in the config is correct and the buttons are being pressed only once.



Versions



  • Python: 3.6.0

  • OS: Raspbian GNU/Linux 9 (stretch)

  • Kivy: 1.10.1









share|improve this question




























    0















    Well, I'm creating a Kivy app in which you can select only one of many statuses. Every status has his own Button and the selected status has a different background_color from the others.



    The problem is that sometimes (apparently random) after clicking a button, two of them stays with his background changed at the same time. The strange thing is that I'm checking the background_color of those elements and it doesn't match with the result that I'm seeing on the screen.



    So, the background_color property has one color but another one is being rendered on the screen.



    Relevant kv file section:



    <StatusButtonsContainer>:
    cols: 2
    spacing: 8
    padding: 0,16,0,0

    <StatusButton>:
    selected: False
    text: self.status_name
    on_release: app.on_change_status_click(self.status_name)
    font_size: '16'
    background_color: self.back_color if self.selected else (0.259, 0.259, 0.259,1)
    background_normal: ''
    background_down: ''
    background_disabled_normal: ''


    This is how I'm creating the Button widgets dinamically:



    class StatusButtonsContainer(GridLayout):

    def __init__(self, **kwargs):
    super(StatusButtonsContainer, self).__init__(**kwargs)
    for name, color in config.statuses.items():
    button = StatusButton(status_name=name, back_color=color)
    self.add_widget(button)


    class StatusButton(Button):
    status_name = StringProperty()
    back_color = ListProperty()


    And this the function that is executed when the button is pressed:



    class ControlsScreen(Screen):

    def change_selected_status(self, status):
    for button in self.ids.buttons_container.children:
    if button.status_name == status:
    button.selected = True
    button.disabled = True
    print('Status ' + button.status_name + ' was selected.')
    print('background_color:' + str(button.background_color))
    else:
    button.selected = False
    button.disabled = status in ['printing', 'preparing', 'paused']
    print('Status ' + button.status_name + ' was NOT selected.')
    print('background_color:' + str(button.background_color))


    It's even more weird that this is happening on a Raspberry Pi 3 with Raspbian, but I'm not able to reproduce it on a Windows machine... I double checked that the [input] section in the config is correct and the buttons are being pressed only once.



    Versions



    • Python: 3.6.0

    • OS: Raspbian GNU/Linux 9 (stretch)

    • Kivy: 1.10.1









    share|improve this question


























      0












      0








      0








      Well, I'm creating a Kivy app in which you can select only one of many statuses. Every status has his own Button and the selected status has a different background_color from the others.



      The problem is that sometimes (apparently random) after clicking a button, two of them stays with his background changed at the same time. The strange thing is that I'm checking the background_color of those elements and it doesn't match with the result that I'm seeing on the screen.



      So, the background_color property has one color but another one is being rendered on the screen.



      Relevant kv file section:



      <StatusButtonsContainer>:
      cols: 2
      spacing: 8
      padding: 0,16,0,0

      <StatusButton>:
      selected: False
      text: self.status_name
      on_release: app.on_change_status_click(self.status_name)
      font_size: '16'
      background_color: self.back_color if self.selected else (0.259, 0.259, 0.259,1)
      background_normal: ''
      background_down: ''
      background_disabled_normal: ''


      This is how I'm creating the Button widgets dinamically:



      class StatusButtonsContainer(GridLayout):

      def __init__(self, **kwargs):
      super(StatusButtonsContainer, self).__init__(**kwargs)
      for name, color in config.statuses.items():
      button = StatusButton(status_name=name, back_color=color)
      self.add_widget(button)


      class StatusButton(Button):
      status_name = StringProperty()
      back_color = ListProperty()


      And this the function that is executed when the button is pressed:



      class ControlsScreen(Screen):

      def change_selected_status(self, status):
      for button in self.ids.buttons_container.children:
      if button.status_name == status:
      button.selected = True
      button.disabled = True
      print('Status ' + button.status_name + ' was selected.')
      print('background_color:' + str(button.background_color))
      else:
      button.selected = False
      button.disabled = status in ['printing', 'preparing', 'paused']
      print('Status ' + button.status_name + ' was NOT selected.')
      print('background_color:' + str(button.background_color))


      It's even more weird that this is happening on a Raspberry Pi 3 with Raspbian, but I'm not able to reproduce it on a Windows machine... I double checked that the [input] section in the config is correct and the buttons are being pressed only once.



      Versions



      • Python: 3.6.0

      • OS: Raspbian GNU/Linux 9 (stretch)

      • Kivy: 1.10.1









      share|improve this question
















      Well, I'm creating a Kivy app in which you can select only one of many statuses. Every status has his own Button and the selected status has a different background_color from the others.



      The problem is that sometimes (apparently random) after clicking a button, two of them stays with his background changed at the same time. The strange thing is that I'm checking the background_color of those elements and it doesn't match with the result that I'm seeing on the screen.



      So, the background_color property has one color but another one is being rendered on the screen.



      Relevant kv file section:



      <StatusButtonsContainer>:
      cols: 2
      spacing: 8
      padding: 0,16,0,0

      <StatusButton>:
      selected: False
      text: self.status_name
      on_release: app.on_change_status_click(self.status_name)
      font_size: '16'
      background_color: self.back_color if self.selected else (0.259, 0.259, 0.259,1)
      background_normal: ''
      background_down: ''
      background_disabled_normal: ''


      This is how I'm creating the Button widgets dinamically:



      class StatusButtonsContainer(GridLayout):

      def __init__(self, **kwargs):
      super(StatusButtonsContainer, self).__init__(**kwargs)
      for name, color in config.statuses.items():
      button = StatusButton(status_name=name, back_color=color)
      self.add_widget(button)


      class StatusButton(Button):
      status_name = StringProperty()
      back_color = ListProperty()


      And this the function that is executed when the button is pressed:



      class ControlsScreen(Screen):

      def change_selected_status(self, status):
      for button in self.ids.buttons_container.children:
      if button.status_name == status:
      button.selected = True
      button.disabled = True
      print('Status ' + button.status_name + ' was selected.')
      print('background_color:' + str(button.background_color))
      else:
      button.selected = False
      button.disabled = status in ['printing', 'preparing', 'paused']
      print('Status ' + button.status_name + ' was NOT selected.')
      print('background_color:' + str(button.background_color))


      It's even more weird that this is happening on a Raspberry Pi 3 with Raspbian, but I'm not able to reproduce it on a Windows machine... I double checked that the [input] section in the config is correct and the buttons are being pressed only once.



      Versions



      • Python: 3.6.0

      • OS: Raspbian GNU/Linux 9 (stretch)

      • Kivy: 1.10.1






      kivy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 14:14







      fsinisi90

















      asked Nov 13 '18 at 21:16









      fsinisi90fsinisi90

      413523




      413523






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Finally, the problem was that I was updating the UI in a thread different from the main thread.



          In my application, the button status can be changed either from the UI directly or from a web socket message. So, when the button's background_color was changed through web socket, the UI update was called by another thread and, for some reason, that was causing the issue.



          I've solved it with the @mainthread decorator:



          @mainthread
          def change_status_ui(self, status):
          self.get_screen('Controls').change_selected_status(status)
          self.get_screen('Status').change_status(status)


          Now, it doesn't matter which thread call this method, it will be executed on the main thread.






          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%2f53289604%2fupdating-buttons-background-color-not-reflected-on-the-ui-sometimes%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














            Finally, the problem was that I was updating the UI in a thread different from the main thread.



            In my application, the button status can be changed either from the UI directly or from a web socket message. So, when the button's background_color was changed through web socket, the UI update was called by another thread and, for some reason, that was causing the issue.



            I've solved it with the @mainthread decorator:



            @mainthread
            def change_status_ui(self, status):
            self.get_screen('Controls').change_selected_status(status)
            self.get_screen('Status').change_status(status)


            Now, it doesn't matter which thread call this method, it will be executed on the main thread.






            share|improve this answer



























              0














              Finally, the problem was that I was updating the UI in a thread different from the main thread.



              In my application, the button status can be changed either from the UI directly or from a web socket message. So, when the button's background_color was changed through web socket, the UI update was called by another thread and, for some reason, that was causing the issue.



              I've solved it with the @mainthread decorator:



              @mainthread
              def change_status_ui(self, status):
              self.get_screen('Controls').change_selected_status(status)
              self.get_screen('Status').change_status(status)


              Now, it doesn't matter which thread call this method, it will be executed on the main thread.






              share|improve this answer

























                0












                0








                0







                Finally, the problem was that I was updating the UI in a thread different from the main thread.



                In my application, the button status can be changed either from the UI directly or from a web socket message. So, when the button's background_color was changed through web socket, the UI update was called by another thread and, for some reason, that was causing the issue.



                I've solved it with the @mainthread decorator:



                @mainthread
                def change_status_ui(self, status):
                self.get_screen('Controls').change_selected_status(status)
                self.get_screen('Status').change_status(status)


                Now, it doesn't matter which thread call this method, it will be executed on the main thread.






                share|improve this answer













                Finally, the problem was that I was updating the UI in a thread different from the main thread.



                In my application, the button status can be changed either from the UI directly or from a web socket message. So, when the button's background_color was changed through web socket, the UI update was called by another thread and, for some reason, that was causing the issue.



                I've solved it with the @mainthread decorator:



                @mainthread
                def change_status_ui(self, status):
                self.get_screen('Controls').change_selected_status(status)
                self.get_screen('Status').change_status(status)


                Now, it doesn't matter which thread call this method, it will be executed on the main thread.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 16:38









                fsinisi90fsinisi90

                413523




                413523



























                    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%2f53289604%2fupdating-buttons-background-color-not-reflected-on-the-ui-sometimes%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

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric