Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kivy操作按钮无法通过python更新图标_Python_Python 3.x_Kivy_Kivy Language - Fatal编程技术网

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

Kivy操作按钮无法通过python更新图标,python,python-3.x,kivy,kivy-language,Python,Python 3.x,Kivy,Kivy Language,我正在使用python和Raspberry pi制作一个应用程序,希望通过操作栏上的图标指示该应用程序是否连接到internet。在构建方法中,它通过调用的“is\u connected”来持续检查该应用程序是否通过“Clock.schedule\u interval(my\u callback.is\u connected,0.5)”连接到internet.但是如果图标在is\u connected方法中连接,我不知道如何更改图标 class Menu(BoxLayout): mana

我正在使用python和Raspberry pi制作一个应用程序,希望通过操作栏上的图标指示该应用程序是否连接到internet。在构建方法中,它通过调用的“is\u connected”来持续检查该应用程序是否通过“Clock.schedule\u interval(my\u callback.is\u connected,0.5)”连接到internet.但是如果图标在is\u 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
#:导入hex kivy.utils.get\u color\u从\u hex
#:进口工厂kivy.Factory.Factory
:
在以下情况之前:
矩形:
pos:self.pos
大小:self.size
经理:屏幕管理器
方向:“垂直”
操作栏:
尺寸:0.15
背景图像:“”
背景颜色:0.349,0.584,0.917,1
ActionView:
以前的行动:
id:actprev
标题:“RheoSb[/b]”
标记:正确
##颜色:0.105,0.109,0.113,1
字体大小:100
应用程序图标:'./assest/viclink2.jpg'
与_previous一起:False
按:root.set\u上一个屏幕()
按:root.btn\u SwipeSound()
操作按钮:
id:motorBtn
文本:“”
图标:'./assest/Ethernet.jpg'
经理:
id:屏幕管理器
:
:
:
:
id:屏幕管理器
问题1-菜单()的双实例 应用程序运行时,会创建两个
class Menu()
实例。第一个实例由
my\u callback=Menu()
创建。第二个实例由
返回菜单()
创建。第一个实例没有关联的模态视图。最重要的是,针对第一个实例安排时钟事件。因此,您将无法更新图标

解决方案 问题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
问题1-菜单()的双实例 应用程序运行时,会创建两个
class Menu()
实例。第一个实例由
my\u callback=Menu()
创建。第二个实例由
返回菜单()
创建。第一个实例没有关联的模态视图。最重要的是,针对第一个实例安排时钟事件。因此,您将无法更新图标

解决方案 问题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

一如既往,你是我最后的希望!!我犯了这样一个新手错误!非常感谢。一如既往,你是我最后的希望!!我犯了这样一个新手错误!非常感谢。
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