Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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
Python 验证Ip地址时,Kivy Label无法刷新_Python_Python 3.x_Kivy_Kivy Language - Fatal编程技术网

Python 验证Ip地址时,Kivy Label无法刷新

Python 验证Ip地址时,Kivy Label无法刷新,python,python-3.x,kivy,kivy-language,Python,Python 3.x,Kivy,Kivy Language,我正在用raspberry pi模块构建一个Kivy接口。在用户界面中,我有一个显示Ip地址的状态屏幕。如果我断开以太网电缆,屏幕会给我错误标签(Kivy screenone label:IpAddress),当我重新连接以太网电缆时,标签应该更新回Ip地址。不幸的是,当我断开以太网电缆时,应用程序显示“错误”,但当我重新连接以太网时,它没有显示我的Ip地址。Ipaddress标签保持为“错误” 问题 未重置属性,在套接字之后的try块内打印ip。创建连接()调用 解决方案 在一级屏幕,执行以下

我正在用raspberry pi模块构建一个Kivy接口。在用户界面中,我有一个显示Ip地址的状态屏幕。如果我断开以太网电缆,屏幕会给我错误标签(Kivy screenone label:IpAddress),当我重新连接以太网电缆时,标签应该更新回Ip地址。不幸的是,当我断开以太网电缆时,应用程序显示“错误”,但当我重新连接以太网时,它没有显示我的Ip地址。Ipaddress标签保持为“错误”

问题 未重置属性,
套接字之后的
try
块内打印ip
。创建连接()
调用

解决方案 在
一级屏幕
,执行以下操作:

  • 移动
    stringIP=subprocess。检查_输出([“主机名”,“-I”])。拆分()[0]
    socket之后。创建_连接()
    调用
  • 将class属性,
    print\u ip=StringProperty(str(stringIP).strip(“b”)替换为
    print\u ip=StringProperty(“”)
  • stringIP=subprocess.check()之后添加
    self.print\u ip=str(stringIP).strip(“b'))

  • self.ids.ipAddress.text
    替换为
    self.print\u ip
片段
在kivy文件中,您正在设置
id:ipAddress
,这意味着标签将查看
ScreenOne
对象的
ipAddress
属性

由于Kivy属性的工作方式,对该属性的任何更改都将自动反映在标签中。因此,在python代码中,不应直接设置标签文本;使用
self.ipAddress.set(“新值”)

错误发生后,您也不会重置标签值,因此即使在try语句中建立了连接,标签仍将保持在错误值

我建议将更新ip功能更改为:

def更新_ip(self,*args):
尝试:
#连接到主机--告诉我们主机是否确实是
#可达
socket.create_连接((“www.google.com”,80))
自打印ip设置(str(stringIP).strip(“b’”)
打印(“ip”)
#打印(“连接”)
返回真值
除操作错误外:
self.print\u ip.set(“[b][color=ff0000]错误[/color][b]”)
打印(“非Ip”)
返回错误

Add
stringIP=subprocess。检查输出([“主机名”,“-I”])。在
socket之间拆分()[0]
。创建连接(…)
self.print\u ip=str(…)
。请参考更新后的帖子了解详细信息。你好,ikolim,如果可以的话,请检查我添加到问题栏的更新二!!干杯将
中的
self.ids.ipAddress.text=…
替换为
self.print\u ip=…
,但OSError:
块除外。同时删除
return True
return False
。它可以用ikolim!!!!它起作用了!!非常感谢。我没有改变,除了你说的OSError:self.print\u ip!!
##this is the root widget
class Menu(BoxLayout):
    manager = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Menu, self).__init__(**kwargs)
        Window.bind(on_keyboard=self._key_handler)
        #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 set_previous_screen(self):
        if self.manager.current != 'home':
            #self.manager.transition.direction = 'left'
            self.manager.transition = SwapTransition()


class ScreenOne(Screen,BoxLayout):

    stringIP=subprocess.check_output(["hostname", "-I"]).split()[0]
    print_ip = StringProperty(str(stringIP).strip("b'"))

    def __init__(self, **kwargs):
        super(ScreenEnergy, self).__init__(**kwargs)
        #Clock.schedule_interval(self.update_ip, 1)
        scheduler1 = BackgroundScheduler()
        scheduler1.add_job(self.update_ip, 'interval', seconds=1)
        scheduler1.start()
        # #self.update_ip()
        print("status window")


    def update_ip(self,*args):     
        try:
            # connect to the host -- tells us if the host is actually
            # reachable
            socket.create_connection(("www.google.com", 80))
            self.print_ip
            print("ip")
            #print("connected")
            return True
        except OSError:
            self.ids.ipAddress.text="[b][color=ff0000]Error[/color][/b]"
            print("not Ip ")
        return False

        # try:
        #     #socket.inet_aton(self.print_ip)
        #     #socket.inet_pton(socket.AF_INET6, self.print_ip)
        #     self.print_ip='\b(([1-9][0-9]?|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
        #     self.ids.ipLabel.text="faffxga"
        #         # legal
        # except socket.error:
        #     self.ids.ipLabel.text="adadadsadsa"
        # return False

        #         # Not legal

       class MenuApp(FlatApp):


    def build(self):
        my_callback=Menu()
        #ip_call =ScreenEnergy()
        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



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:

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

    Manager:
        id: screen_manager

<ScreenOne>:
    BoxLayout:
        orientation: 'vertical'
        WrappedLabel:
            id:welcomeStatus
            text: "[b]Status[/b]"
            font_size:min(root.width,root.height)/15
        GridLayout:
            cols:2
            row: 3
            padding:root.width*.02,root.height*.03
            spacing:min(root.width,root.height)*.02
            Label:   
                id:ipLabel
                size_hint_x: .15
                color: 0, 0, 0, 1
                text: '[b]IP Address :[/b]'
                markup:True 
            Label:
                id:ipAddress
                size_hint_x: .15
                color: 0, 0, 0, 1
                text: root.print_ip
                color: 1, 0, 0, 1
                bold:True
                markup:True  

<Screen 2>:

<Manager>:

    id: screen_manager
class ScreenOne(Screen):

    print_ip = StringProperty('')
    ...

    def update_ip(self,*args):     
        try:
            # connect to the host -- tells us if the host is actually
            # reachable
            socket.create_connection(("www.google.com", 80))
            stringIP = subprocess.check_output(["hostname", "-I"]).split()[0]
            self.print_ip = str(stringIP).strip("b'")
            print(f"ip={self.print_ip}")
        except OSError:
            self.print_ip = "[b][color=ff0000]Error[/color][/b]"
            print("not Ip ")