Kivy无法更新操作按钮图标

时间:2019-05-02 05:05:47

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

我正在用树莓派模块构建Kivy接口。不幸的是,当操作栏上的以太网断开连接时,我无法更改图标。当以太网电缆连接时,我已经分配了一个图标,但是断开连接时,它不会更新我的Asset文件夹中的图标。当我反复使用时,基本应用程序可以工作相同的图标在Kivy文件上。(EE1.png和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'

但是当我更改

except OSError:

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

`except OSError:

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

它显示mm.png图标。

编辑 代替

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()

我使用*BackgroundScheduler()*回调了*is_connected()*方法,这给我带来了问题。您能解释一下为什么发生这种情况吗!!

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

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

在代码from apscheduler.schedulers.background import BackgroundScheduler中,我只是导入该库并开始使用它。从来没有想过使用奇异时钟时间表。

1 个答案:

答案 0 :(得分:2)

在下面的示例中工作正常。测试环境为笔记本电脑和WiFi / WLAN(无线局域网)。

示例

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

输出

Internet Connected Internet Disconnected