使用Kivy作为用户界面,希望像PLC一样监控IO

使用Kivy作为用户界面,希望像PLC一样监控IO,io,kivy,plc,Io,Kivy,Plc,我想监控IO并在我的Kivy标签上显示状态 我知道如何更新Kivy标签和操作按钮,但我想显示标签状态,如“如果X输入关闭,则显示Y输出关闭” 我的想法是,我想不断地监控这些。在读取时间之间可能有2秒或更长的延迟,但其操作方式与PLC类似 我的想法是创建一个对象,以时间为基础在数据上循环,当满足critera时,我可以调用主类并更新标签。我的问题是,我不知道如何在我的kivy应用程序生成后调用对象创建,以便我可以监视IO 如果你能帮助我,我将不胜感激。这是我的代码:main.py from kiv

我想监控IO并在我的Kivy标签上显示状态

我知道如何更新Kivy标签和操作按钮,但我想显示标签状态,如“如果X输入关闭,则显示Y输出关闭”

我的想法是,我想不断地监控这些。在读取时间之间可能有2秒或更长的延迟,但其操作方式与PLC类似

我的想法是创建一个对象,以时间为基础在数据上循环,当满足critera时,我可以调用主类并更新标签。我的问题是,我不知道如何在我的kivy应用程序生成后调用对象创建,以便我可以监视IO

如果你能帮助我,我将不胜感激。这是我的代码:main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
import random
import RPi.GPIO as GPIO
from kivy.event import EventDispatcher

class YourWidget(Widget):

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(21, GPIO.OUT)

    random_number = StringProperty()
    button4status = StringProperty()
    button4statusinput = StringProperty()



    def __init__(self, **kwargs):
            super(YourWidget, self).__init__(**kwargs)
            self.random_number = str(random.randint(1, 100))
        #self.random_number2 = str(random.randint(1, 100))
        self.button4status = str("Light is OFF")

        def change_text(self):
            self.random_number = str(random.randint(1, 100))

    def change_text2(self, button4statusinput):
            #self.random_number2 = str(random.randint(1, 100))
        self.button4status = str(button4statusinput)

    def button4pressed(self):
        #GPIO.setmode(GPIO.BCM)
        #GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_OFF
        #GPIO.setup(21, GPIO.OUT)
        GPIO.output(21, False)
        button4statusinput = "Light is ON"

    def button4released(self):
        #GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
        #GPIO.setup(21, GPIO.OUT)
        GPIO.output(21, True)
        button4statusinput = "Light is OFF"

    def ButtonMonitor(self):
        GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
        if GPIO.input(6) == RPI.GPIO.HIGH:
            GPIO.output(21, True)

    #def make_buttontester1():
        #pass

    def ExitApp(Self):
        GPIO.cleanup()
        App.get_running_app().stop()

#class buttontester1(object):
        #GPIO.setmode(GPIO.BCM)
        #GPIO.setup(16, GPIO.OUT)
        #GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_OFF)

        #while True:
            #if GPIO.input(6) == GPIO.LOW:
                #GPIO.output(16, True)
            #if GPIO.input(6) == GPIO.HIGH:
                #GPIO.output(16, False)





class YourApp(App):
        def build(self):
            return YourWidget()

if __name__ == '__main__':
    YourApp().run()
你的千伏

#:kivy 1.9.2

<YourWidget>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: 'vertical'
        spacing: 10
        padding: 10

        BoxLayout:
            orientation: 'horizontal'
            spacing: 10
            padding: 10
            Button:
                id: btn1
                text: "Change Text"
                on_release: root.change_text()
                size_hint: 1, 1

            Button:
                id: btn2
                text: "Exit App"
                on_release: root.ExitApp()
                size_hint: 1, 1

            Label:
                id: lbl1
                font_size: 70
                text:root.random_number
                size_hint: 1, 1

        BoxLayout:
            orientation: 'horizontal'
            spacing: 10
            padding: 10
            Button:
                id: btn3
                text: "Change Text"
                on_release: root.change_text2()
                size_hint: 1, 1

            Button:
                id: btn4
                text: "Press to Light LED"
                on_press: root.button4pressed()
                on_press: root.change_text2(str("Light Is ON"))
                on_release: root.button4released()
                on_release: root.change_text2(str("Light is OFF"))
                size_hint: 1, 1

            Label:
                id: lbl2
                font_size: 70
                text:root.button4status
                size_hint: 1, 1
#:kivy 1.9.2
:
盒子布局:
大小:root.size
pos:root.pos
方向:“垂直”
间距:10
填充:10
盒子布局:
方向:“水平”
间距:10
填充:10
按钮:
id:btn1
文本:“更改文本”
发布时:root.change\u text()
大小提示:1,1
按钮:
id:btn2
文本:“退出应用程序”
发布时:root.ExitApp()
大小提示:1,1
标签:
id:lbl1
字体大小:70
文本:root.random\u编号
大小提示:1,1
盒子布局:
方向:“水平”
间距:10
填充:10
按钮:
id:btn3
文本:“更改文本”
发布时:root.change\u text2()
大小提示:1,1
按钮:
id:btn4
文字:“按下可点亮LED”
按下按钮:root.button4pressed()
按:root.change\u text2(str(“灯亮”))
发布时:root.button4released()
发布时:root.change\u text2(str(“灯熄灭”))
大小提示:1,1
标签:
id:lbl2
字体大小:70
文本:root.button4status
大小提示:1,1

谢谢大家!

例如,我希望button1tester类在循环中运行并评估IO。在kivy应用程序中,它正常工作,但在后台也有类似循环对象的功能,那么还有什么方法可以与kivy应用程序并行执行呢?你应该使用线程来执行此类后台任务。如果任务不需要很长时间,您也可以使用
Clock.schedule\u interval
。感谢您使用的线程在这里工作得非常好。现在我遇到了一个问题,我需要从线程访问我的函数。啊!