Nerd For Tech
Published in

Nerd For Tech

How to Create a Back Button in Kivy

I created a back button by first setting up an __init__ function in my App class and then having self.previous_screen that would hold the name of the previous screen. I used <Screen> in the .kv file to make this apply to all the screens. Then whenever I created a back button, I would reference app.previous_screen.

The main.py file:

from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.app import App, Builder

Builder.load_file("medium.kv")

class Home(Screen):
pass
class User(Screen):
pass

class RootWidget(ScreenManager):
pass

class Test(App):
def __init__(self, **kwargs):
super(Test, self).__init__(**kwargs)
self.previous_screen = ""

def build(self):
return RootWidget()

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

I used <Screen> and the on_leave property to make sure that the self.previous_screen updated after every screen change.

<Screen>:
on_leave: app.previous_screen = self.name

<Home>:
name: "home_screen"
GridLayout:
cols: 1
padding: 10
Button:
text: "To User Screen"
on_release: root.manager.current = "user_screen"

<User>:
name: "user_screen"
GridLayout:
cols: 1
padding: 10
Button:
text: "Back"
on_release: root.manager.current = app.previous_screen

<RootWidget>:
Home
User

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

You Don’t Need a Bazooka to Hunt Rabbits

Kotlin in the real world

Organizations often use the terms ‘Quality Assurance’ (QA) vs ‘Quality Control’ (QC)…

Non-Preemptive Priority Scheduling

priority

Things to consider for mobile app testing

Essential guide to effective goal setting for your mobile development project

Building AWS Infra with Terraform 2

Prepare Skeleton of our Micro-services and Setup Eureka Service Application

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rachela

Rachela

I am a librarian and new computer programmer creating an app with Python and Kivy to document the plants in my garden.

More from Medium

Instabot get all the followers of an Instagram account with Python

How to export excel properly with Flask and Ajax Call

Build Your First Transliterator Text Editor with Python Tkinter

Backend with Python: