Python Kivy - ActionBar on all ScreenManager with GPIO

Viewed 30

I try to add actionbar to all screenmanagers in Kivy 2.1.0. I found some solution to my problem but I added some more features like GPIO pinout from Raspberry Pi. Here is a solution without gpio.

I try to code with BoxLayout in the main.py but when I click the button, nothing happens. Has the code any logical problem? Could you check it, please?

Here main.py

`


from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
import RPi.GPIO as GPIO
from kivy.clock import Clock
from kivy.lang.builder import Builder

up=4
down=17
GPIO.setwarnings(False)                                         
GPIO.setmode(GPIO.BCM)                                          # BCM: use the gpio numbers, BOARD :use physical pin numbering

pulling=GPIO.PUD_UP                                             
GPIO.setup(up,      GPIO.IN, pull_up_down=pulling)              
GPIO.setup(down,    GPIO.IN, pull_up_down=pulling)                 

class WelcomeScreen(Screen):
    pass

class FirstScreen(Screen):
    pass

class SecondScreen(Screen):
    pass

class ScreenManager(ScreenManager):
    pass

class BoxTest(BoxLayout):
    
    def buttonUp(self, channel):
        print('2. message on screen')
        Clock.schedule_once(self.buttonUp)
        
    def buttonDown(self, channel):
        print('2. message on screen')
        Clock.schedule_once(self.buttonDown)

    class BoxTest:

        def __init__(self):
            self.state = BoxTest.PageHome(self)

        def buttonUp(self,dt):                                          
            self.state = self.state.buttonUp(self) 

        def buttonDown(self,dt):                                         
            self.state = self.state.buttonDown(self) 

        class State:
            def __init__(self, dt):
                pass

        class PageHome(State):        
            def buttonUp(self, dt):
                App.get_running_app().screenUp('second')
                return BoxTest.PageSecond(self)
            def buttonDown(self, dt):
                App.get_running_app().screenDown('first')
                return BoxTest.PageFirst(self)

        class PageFirst(State):
            def buttonUp(self, dt):
                App.get_running_app().screenUp('welcome')
                return BoxTest.PageHome(self)
            def buttonDown(self, dt):
                App.get_running_app().screenDown('second')
                return BoxTest.PageSecond(self)
        
        class PageSecond(State):
            def buttonUp(self, dt):
                App.get_running_app().screenUp('first')
                return BoxTest.PageFirst(self)
            def buttonDown(self, dt):
                App.get_running_app().screenDown('welcome')
                return BoxTest.PageHome(self)

Builder.load_file("main.kv")

class TestApp(App):
    boxtest= BoxTest() 
    
    def screenUp(self, screenName):                                                            
        self.manager.transition.direction = 'up'
        self.manager.current = screenName

    def screenDown(self, screenName):                                                           
        self.manager.transition.direction = 'down'
        self.manager.current = screenName

    def buttonUp(self, channel):
        print('1. message on screen')
        Clock.schedule_once(self.boxtest.buttonUp)
        
    def buttonDown(self, channel):
        print('1. message on screen')
        Clock.schedule_once(self.boxtest.buttonDown)

    def build(self):                                                                       
        self.manager = ScreenManager()
        self.boxtest = BoxTest()

        GPIO.add_event_detect(up,           GPIO.FALLING, callback=self.buttonUp,       bouncetime=1)                               # start button definition
        GPIO.add_event_detect(down,         GPIO.FALLING, callback=self.buttonDown,     bouncetime=1)                               # stop button definition

        return self.boxtest

if __name__ == '__main__':
    TestApp().run()

and here main.kv

`

#:kivy 2.1.0
#:import sp kivy.metrics.sp
#:import dp kivy.metrics.dp

<BoxTest>:
    orientation: 'vertical'

    canvas.before:
        Color:
            rgb: .6, .6, .6
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'background.png'

    ScreenManager:
        id: sm
        canvas.before:
            Color:
                rgb: .7, .5, .2, 1
            Rectangle:
                pos: self.pos
                size: self.size
        WelcomeScreen:
        FirstScreen:
        SecondScreen:

    SomeMenu_ActionBar:
        id: ActionBar

<WelcomeScreen>:
    name: 'welcome'
    Label:
        text: 'Welcome Screen'
        font_size: sp(50)

<FirstScreen>:
    name: 'first'
    Label:
        text: 'First Screen'
        font_size: sp(50)

<SecondScreen>:
    name: 'second'
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Second Screen'
            font_size: 50

        BoxLayout:
            Button:
                text: 'Boxlayout 1'
                font_size: 30
            Button:
                text: 'Boxlayout 2'
                font_size: 30

<SomeMenu_ActionBar@ActionBar>:

    pos: (0,0)
    size_hint_y:0.1
    size: root.width , 50
    background_color: (.5,.5,.5,.5)
    ActionView:
        id:av
        ActionPrevious:
            with_previous: False
            app_icon: ''   # Set the Icon to an empty String  
        
        ActionButton:
            text: 'Number One'
            size_hint_x: 1
            font_size: 25
            color: (0,0,1,1)

        ActionButton:
            text: 'Number Two'
            font_size: 25
            color: (0,1,0,1)
            size_hint_x: 1

        ActionButton:
            #   id: micro_text
            text: 'Number Three'
            font_size: 25
            color: (1,0,0,1)
            size_hint_x: 1

If any help, would be nice. Thank you for helping.

0 Answers
Related