Kivy Action按钮无法通过python更新图标

时间:2019-04-10 02:45:03

标签: python python-3.x kivy kivy-language

我正在使用python和Raspberry pi制作一个应用程序,并想在操作栏上使用图标指示该应用程序是否连接到互联网。在构建方法中,它会不断检查该应用程序是否通过“ 通过调用“ is_connected”来设置Clock.schedule_interval(my_callback.is_connected,0.5)”。但是,如果在is_connected方法内部连接了图标,我不会更改其图标。 >

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

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


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

    def set_previous_screen(self):
        if self.manager.current != 'home':
            self.manager.transition = SwapTransition()
            self.manager.current = 'home'
    def btn_SwipeSound(self):
        sound = SoundLoader.load('./assest/arrow.wav')
        if sound:

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

        try:
            # connect to the host -- tells us if the host is actually
            # reachable
            socket.create_connection(("www.google.com", 80))
            self.motorBtn.icon = './assest/usb.jpg

            print ("connected")
            return True  
        except OSError:
            pass
    self.motorBtn.icon = './assest/usb1.jpg
        print("not connected")
        return False


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

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


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

#:kivy 1.10.0
#:import hex kivy.utils.get_color_from_hex
#:import Factory kivy.factory.Factory

<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/viclink2.jpg'
                with_previous: False
                on_press: root.set_previous_screen()
                on_press: root.btn_SwipeSound()

            ActionButton:
                id:motorBtn
                text:''
                icon:'./assest/Ethernet.jpg'

    Manager:
        id: screen_manager

<Screen 1>:

<Screen 2>:

<Screen 3>:

<Manager>:

    id: screen_manager

1 个答案:

答案 0 :(得分:0)

问题1-Menu()的双重实例

应用运行时,将创建两个class Menu()的实例。第一个实例由my_callback = Menu()创建。第二个实例由return Menu()创建。第一个实例没有与之关联的模式视图。最重要的是,时钟事件是针对第一个实例安排的。因此,您将无法更新图标。

解决方案

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

问题2-更新ActionButton的图标

问题是由于不正确的引用/对ActionButton的访问。

解决方案

使用self.ids.motorBtn.icon

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

    try:
        # connect to the host -- tells us if the host is actually
        # reachable
        socket.create_connection(("www.google.com", 80))
        self.ids.motorBtn.icon = './assest/usb.jpg'

        print("connected")
        return True
    except OSError:
        pass
    self.ids.motorBtn.icon = './assest/usb1.jpg'
    print("not connected")
    return False