0

This should be a pretty easy fix. I'm new to Kivy. I'm trying to have the canvas cleared on a button press, and then display a new widget to essentially move to another page. When I run it and press the button, the canvas is cleared, but I get nothing from IntroPage.

Python Script:

import kivy
kivy.require('2.0.0')

from kivy.app import App

from kivy.uix.label import Label

from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty


class ABT(App):
    def build(self):
        return WelcomePage()


class WelcomePage(Widget):
    def btn(self):
        self.canvas.clear()
        print('pressed')
        return IntroPage()


class IntroPage(Widget):
    def __init__(self):
    pass


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

KV File:

<WelcomePage>
    canvas.before:
        Color:
            rgba: (.43,.51,.92,.26)
        Rectangle:
            pos: self.pos
            size: self.size
    GridLayout:
        cols:1
        size: root.width, root.height
        Image:
            source: 'abt1t.png'
            size_hint: (1,.8)

        Button:
            text:"begin"
            background_color: (.43,.51,.92,.26)
            size_hint: (1,.2)
            on_press: root.btn()

<IntroPage>
    GridLayout:
        cols:1
        Label:
            text:"This won't show up!"
Capedantic
  • 13
  • 4

1 Answers1

0

Returning IntroPage from btn won't work, as btn isn't a build method.

The best way to implement multiple pages is probably to use ScreenManager:

from kivy.uix.screenmanager import ScreenManager, Screen


class ABT(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(WelcomePage(name="welcome"))
        sm.add_widget(IntroPage(name="intro"))
        return sm


class WelcomePage(Screen):
    def btn(self):
        print('pressed')
        self.manager.current = "intro"


class IntroPage(Screen):
    def __init__(self):
        pass
TheInitializer
  • 566
  • 7
  • 20
  • Thank you for your quick reply. I've implemented this and get an error. (kivy.uix.screenmanager.ScreenManagerException: No Screen with name "intro") I've also tried `class ABT(App): def build(self): sm = ScreenManager() welcome_screen = Screen(name="welcome") sm.add_widget(welcome_screen) intro_screen = Screen(name="intro") sm.add_widget(intro_screen) return sm` which should achieve the same (I think), but this doesn't serve either when I run it. – Capedantic May 30 '21 at 03:51
  • Played around a bit and got it working. The biggest change I made was using `Builder.load_string('')` instead of having them separate, which is a little annoying, but it works. Thanks again! – Capedantic May 30 '21 at 04:15