python kivy - on_press выполняется дважды - PullRequest
0 голосов
/ 29 января 2020

Новичок ie до Python - Я пишу приложение Raspberry Pi с сенсорным экраном. Можно заставить его работать, но функция увеличения выполняется дважды при нажатии кнопки + (то же самое для уменьшения). Эти кнопки доступны только при нажатии другой кнопки, но это не проблема. Любая помощь, пожалуйста?

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
Window.show_cursor = False
import serial
import time

port = serial.Serial("/dev/ttyS0", baudrate=19200, timeout=3.0)

btnNum = '0'
b1Tmp = 0; b2Tmp = 0; b3Tmp = 0; b4Tmp = 0; b5Tmp = 0; b6Tmp = 0; b7Tmp = 0; b8Tmp = 0
b1Max =50; b2Max =50; b3Max =50; b4Max =50; b5Max =50; b6Max =50; b7Max =50; b8Max =50;


class Container(Widget):
    labR1= ObjectProperty(None)
    labR2= ObjectProperty(None)
    labR3= ObjectProperty(None)
    labR4= ObjectProperty(None)
    labR5= ObjectProperty(None)
    labR6= ObjectProperty(None)
    labR7= ObjectProperty(None)
    labR8= ObjectProperty(None)
    butUP= ObjectProperty(None)
    butDN= ObjectProperty(None)
    labM1= ObjectProperty(None)
    labM2= ObjectProperty(None)
    labM3= ObjectProperty(None)
    labM4= ObjectProperty(None)
    labM5= ObjectProperty(None)
    labM6= ObjectProperty(None)
    labM7= ObjectProperty(None)
    labM8= ObjectProperty(None)



    def bpressed(self,instance):
        global btnNum
        btnNum = instance.text
        self.butDN.disabled = False
        self.butUP.disabled = False

    def breleased(self,instance):
        global btnNum
        btnNum = '0'
        self.butDN.disabled = True
        self.butUP.disabled = True

    def increment(self,instance):
        global btnNum,b1Max,b2Max,b3Max,b4Max,b5Max,b6Max,b7Max,b8Max
        if (btnNum == '1'):
            if (b1Max<100): b1Max+=2
            self.labM1.text = str(b1Max)
        if (btnNum == '2'):
            if (b2Max<100): b2Max=b2Max+1
            self.labM2.text = str(b2Max)
        if (btnNum=='3'):
            if (b3Max<100): b3Max+=1
            self.labM3.text = str(b3Max)
        if (btnNum=='4'):
            if (b4Max<100): b4Max+=1
            self.labM4.text = str(b4Max)
        if (btnNum=='5'):
            if (b5Max<100): b5Max+=1
            self.labM5.text = str(b5Max)
        if (btnNum=='6'):
            if (b6Max<100): b6Max+=1
            self.labM6.text = str(b6Max)
        if (btnNum=='7'):
            if (b7Max<100): b7Max+=1
            self.labM7.text = str(b7Max)
        if (btnNum=='8'):
            if (b8Max<100): b8Max+=1
            self.labM8.text = str(b8Max)

    def decrement(self,instance):
        global btnNum,b1Max,b2Max,b3Max,b4Max,b5Max,b6Max,b7Max,b8Max
        if (btnNum == '1'):
            if (b1Max>20): b1Max-=1
            self.labM1.text = str(b1Max)
        if (btnNum == '2'):
            if (b2Max>20): b2Max-=1
            self.labM2.text = str(b2Max)
        if (btnNum=='3'):
            if (b3Max>20): b3Max-=1
            self.labM3.text = str(b3Max)
        if (btnNum=='4'):
            if (b4Max>20): b4Max-=1
            self.labM4.text = str(b4Max)
        if (btnNum=='5'):
            if (b5Max>20): b5Max-=1
            self.labM5.text = str(b5Max)
        if (btnNum=='6'):
            if (b6Max>20): b6Max-=1
            self.labM6.text = str(b6Max)
        if (btnNum=='7'):
            if (b7Max>20): b7Max-=1
            self.labM7.text = str(b7Max)
        if (btnNum=='8'):
            if (b8Max>20): b8Max-=1
            self.labM8.text = str(b8Max)


    def  serialsend(self,instance):
        port.write("\r\nSay something:".encode())

    def close(self , obj):
        App.get_running_app().stop()
        Window.close()



class BoilerApp(App):

    def build(self):
        self.title = 'Boiler Monitor'
        return Container()

if __name__ == "__main__":
    from kivy.core.window import Window
    Window.fullscreen = True
    app = BoilerApp()
    app.run()

KV файл

<MyButton@Button>:
    color: .8,.9,0,1
    font_size: 30

<MyLabel@Label>:
    color: .8,.9,0,1
    font_size: 30


<Container>

    labR1: labR1
    labR2: labR2
    labR3: labR3
    labR4: labR4
    labR5: labR5
    labR6: labR6
    labR7: labR7
    labR8: labR8

    butUP: butUP
    butDN: butDN

    labM1: labM1
    labM2: labM2
    labM3: labM3
    labM4: labM4
    labM5: labM5
    labM6: labM6
    labM7: labM7
    labM8: labM8


    GridLayout:
        cols: 1
        size: root.width, root.height
        Label:
            text: "Boiler Alarm"
            size_hint: .5, .2
            font_size: 40
            background_color: 1,0,0,1
        GridLayout:
            cols:10
            MyLabel:
                text: "Boiler"
            MyButton:
                id: butB1
                text: "1"
                on_press: root.bpressed(self)
                on_release: root.breleased(self)
            MyButton:
                id: butB2
                text: "2"
                on_press: root.bpressed(self)
                on_release: root.breleased(self)
            MyButton:
                id: butB3
                text: "3"
                on_press: root.bpressed(self)
                on_release: root.breleased(self)
            MyButton:
                id: butB4
                text: "4"
                on_press: root.bpressed(self)
                on_release: root.breleased(self)
            MyButton:
                id: butB5
                text: "5"
                on_press: root.bpressed(self)
                on_release: root.breleased(self)
            MyButton:
                id: butB6
                text: "6"
                on_press: root.bpressed(self)
                on_release: root.breleased(self)
            MyButton:
                id: butB7
                text: "7"
                on_press: root.bpressed(self)
                on_release: root.breleased(self)
            MyButton:
                id: butB8
                text: "8"
                on_press: root.bpressed(self)
                on_release: root.breleased(self)
            MyLabel:
                text: ""
            Label:
                text: "Reading"
                font_size: 25
            MyLabel:
                id: labR1
                text: "0"
            MyLabel:
                id: labR2
                text: "0"
            MyLabel:
                id: labR3
                text: "0"
            MyLabel:
                id: labR4
                text: "0"
            MyLabel:
                id: labR5
                text: "0"
            MyLabel:
                id: labR6
                text: "0"
            MyLabel:
                id: labR7
                text: "0"
            MyLabel:
                id: labR8
                text: "0"
            MyButton:
                id: butUP
                text: "+"
                disabled: True
                on_press: root.increment(self)

            Label:
                text: "Minimum"
                font_size: 25
            MyLabel:
                id: labM1
                text: "50"
            MyLabel:
                id: labM2
                text: "50"
            MyLabel:
                id: labM3
                text: "50"
            MyLabel:
                id: labM4
                text: "50"
            MyLabel:
                id: labM5
                text: "50"
            MyLabel:
                id: labM6
                text: "50"
            MyLabel:
                id: labM7
                text: "50"
            MyLabel:
                id: labM8
                text: "50"
            MyButton:
                id: butDN
                text: "-"
                disabled: True
                on_release: root.decrement(self)

        Button:
            id: btnexit
            text: "Exit"
            size_hint: .5, .1
            on_press: root.close(self)
...