Boolean007
Boolean007

Reputation: 71

Kivy Unable to Update the action button Icon

I'm building a Kivy interface with raspberry pi module. Unfortunately I'm unable to change the icon when Ethernet disconnected on Action bar.I already assign a Icon when Ethernet cable connected but when disconnected it doesn't update the icon in my Asset folder.Basically app works when i use over and over use the same Icon on Kivy file.(EE1.png and mm.png).

class Menu(BoxLayout):
    manager = ObjectProperty(None)
    

    def __init__(self, **kwargs):
        super(Menu, self).__init__(**kwargs)
        Window.bind(on_keyboard=self._key_handler)
        logger.setErrorIcon = self.setErrorIcon
        #btn1 = ActionButton(text='',icon='./assest/usb.jpg')

    def _key_handler(self, instance, key, *args):
        if key is 27:
            self.set_previous_screen()
            return True

            
    def is_connected(self, *args):
        
        motorBtn = StringProperty()
        index = NumericProperty(-1)
        
        try
            socket.create_connection(("www.google.com", 80))
            self.ids.EthBtn.icon = './Asset/EE1.png'
            logger.debug("connected")
           
        except OSError:
            
            self.ids.EthBtn.icon = './Asset/EE.png'<<<<This Icon doesnt shows
	    logger.error("E81:not connected")
      

ActionBar:
        
        size_hint_y: 0.15
        background_image: ''
        background_color: 0.349, 0.584, 0.917, 1
        ActionView:
            ActionPrevious:
                id: actprev
                title: "[b]RheoSB[/b]"
                markup: True
                ##color: 0.105, 0.109, 0.113,1
                font_size: 100
                #app_icon: './assest/v3.jpg'
                app_icon: './Asset/VL2.png'
                with_previous: False
                ##on_release: root.set_previous_screen()
                #on_press: root.manager.current= 'home'
                on_press: root.set_previous_screen()
                on_press: root.btn_SwipeSound()

             
            ActionButton:
                id:errorBtn
                text:''
                icon: ''
                on_press:root.error_logPopup()
               
            ActionButton:
                id:EthBtn
                important: True
                text:''
                icon: './Asset/EE1.png'<<<<<

            ActionButton:
                id:motorBtn
                text:''
                icon: './Asset/mm.png'

But When I Change the

except OSError:

            self.ids.EthBtn.icon = './Asset/EE.png'

to

`except OSError:

         self.ids.EthBtn.icon = './Asset/mm.png'`

it shows the mm.png icon.

EDIT instead of

class MenuApp(App):
index = NumericProperty(-1)

def build(self):
    my_callback = Menu()
    Clock.schedule_interval(my_callback.is_connected, 0.5)
    return my_callback    # Menu()

I used *BackgroundScheduler()* to callback *is_connected()* method this cause me the issue. Could you explain me why this happens please!!

class MenuApp(FlatApp):


def build(self):
    my_callback=Menu()
    scheduler = BackgroundScheduler()
    scheduler.add_job(my_callback.is_connected, 'interval', seconds=1)
    #scheduler.add_job(ip_call.update_ip, 'interval', seconds=1)
    scheduler.start()
    return my_callback

EDIT EDIT https://apscheduler.readthedocs.io/en/latest/userguide.html#basic-concepts https://apscheduler.readthedocs.io/en/latest/modules/schedulers/background.html#apscheduler.schedulers.background.BackgroundScheduler

in the code from apscheduler.schedulers.background import BackgroundScheduler i just import the library and start to use it tbh. Never thought about to use the kivy clock schedule before..

Upvotes: 1

Views: 169

Answers (1)

ikolim
ikolim

Reputation: 16031

It works fine with the example below. The test environment is laptop and WiFi / WLAN (Wireless Local Area Networking).

Example

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty, ObjectProperty, StringProperty
from kivy.core.window import Window
from kivy.clock import Clock
import socket
from kivy.lang import Builder
from kivy.logger import Logger


class Menu(BoxLayout):
    manager = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Menu, self).__init__(**kwargs)
        Window.bind(on_keyboard=self._key_handler)
        # Logger.setErrorIcon = self.setErrorIcon

    def _key_handler(self, instance, key, *args):
        if key is 27:
            self.set_previous_screen()
            return True

    def is_connected(self, *args):
        motorBtn = StringProperty()
        index = NumericProperty(-1)

        try:
            socket.create_connection(("www.google.com", 80))
            self.ids.EthBtn.icon = './Asset/EE1.png'
            Logger.debug("connected")
        except OSError:
            self.ids.EthBtn.icon = './Asset/EE.png'
            Logger.error("E81:not connected")


Builder.load_file('main.kv')


class MenuApp(App):
    index = NumericProperty(-1)

    def build(self):
        my_callback = Menu()
        Clock.schedule_interval(my_callback.is_connected, 0.5)
        return my_callback    # Menu()


if __name__ == '__main__':
    MenuApp().run()

main.kv

<Menu>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size

    manager: screen_manager
    orientation: "vertical"

    ActionBar:

        size_hint_y: 0.15
        background_image: ''
        background_color: 0.349, 0.584, 0.917, 1

        ActionView:
            ActionPrevious:
                id: actprev
                title: "[b]RheoSb[/b]"
                markup: True
                ##color: 0.105, 0.109, 0.113,1
                font_size: 100
                #app_icon: './assest/v3.jpg'
                app_icon: './Asset/VL2.png'
                with_previous: False
                ##on_release: root.set_previous_screen()
                #on_press: root.manager.current= 'home'
                on_press: root.set_previous_screen()
                on_press: root.btn_SwipeSound()

            ActionButton:
                id:errorBtn
                text:''
                icon: ''
                on_press:root.error_logPopup()

            ActionButton:
                id:EthBtn
                important: True
                text:''
                icon: './Asset/EE1.png'

            ActionButton:
                id:motorBtn
                text:''
                icon: './Asset/mm.png'

    ScreenManager:
        id: screen_manager

Output

Internet Connected Internet Disconnected

Upvotes: 2

Related Questions