使用按钮kivy增加,减少和清除

时间:2016-03-17 18:35:50

标签: python kivy

我是初学者,请原谅这个过分简单的问题。我正在尝试创建一个显示计数的控制屏幕。基本上我希望它在其中一个GPIO上获得一个低信号并提前一个整数。我还希望能够使用“+”,“ - ”和“清除”按钮编辑计数。这是我写过的第一批代码之一,所以我可能会离开这里!这是.py的代码:

from kivy.app import App 
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout

import RPi.GPIO as GPIO

startPin = 17

GPIO.setmode(GPIO.BCM)
GPIO.setup(startPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

class InputButton(Button):
    def update(self, dt):
        if GPIO.input(startPin) == True:
        self.ids.textbox.text +=1

class MainWidget(FloatLayout):

    def adv_callback(self, text):
        self.ids.textbox.text += 1

    def fall_callback(self, text):
        self.ids.textbox.text -= 1

    def clear_callback(self, text):
        self.ids.textbox.text = 0


class MainApp(App):
    '''docstring for MainApp'''
    def build(self):
        return MainWidget()


if __name__=="__main__":
    MainApp().run()

和.kv

# File name main.py

<MainWidget>:

canvas.before:
    Color:
        rgba: .95, .95, 1, .7
    Rectangle:
        pos: self.pos
        size: self.size
Button:
    id: clear
    text: 'Clear'
    font_size: '50sp'
    pos: (650, 0)
    size_hint: .2, .3
    on_press: root.clear_callback(self.text) 

Button:
    id: add
    text: '+'
    font_size: '50sp'
    pos: (0, 142)
    size_hint: .2, .3
    on_press: root.adv_callback(self.text)

Button:
    id: less
    text: '-'
    font_size: '50sp'
    pos: (0, 0)
    size_hint: .2, .3
    on_press: root.fall_callback(self.text)

AsyncImage:
    source: '/home/sysop/Pictures/Source_Files_Pocono_logo_on_White_Bg.gif'
    size_hint: .5, .3
    pos: (200, 285)

TextInput:
    id: textbox
    multiline: False
    readonly: True
    size_hint: .5, .3
    font_size: '80sp'
    pos: (325, 0)

ActionBar:
    pos: (0, 432)
    ActionView:
        ActionPrevious:
            title: ""
            with_previous: False
        ActionOverflow:
            ActionButton:
                text: "Shutdown"
                on_press: app.open_settings()

0 个答案:

没有答案