【问题标题】:Kivy Action button unable to update the icon through pythonKivy Action按钮无法通过python更新图标
【发布时间】:2019-04-10 02:45:03
【问题描述】:

我正在使用 python 和 Raspberry pi 制作一个应用程序,如果应用程序连接到互联网,我想在操作栏上显示一个图标。在构建方法中,它不断检查应用程序是否通过“ 连接到互联网” Clock.schedule_interval(my_callback.is_connected, 0.5)" 通过调用 "is_connected"。 但我不知道如何更改图标,如果它在 is_connected 方法中连接。 p>

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

【问题讨论】:

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


    【解决方案1】:

    问题 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
    

    【讨论】:

    • 一如既往,你是我最后的希望!!我犯了这样一个菜鸟的错误!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多