'Change screen in KIVY
I'm putting together a Kivy app and I'm having trouble figuring out how to change screens at an arbitrarily chosen point within the python code.
In the following example, I would like to know how to switch from Screen2 back to Screen1 by executing a function my main.py file.
Here's my main.py:
# Imports
import csv
import numpy as np
from pyzbar.pyzbar import decode
from barcode import UPCA
from PIL import Image
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.camera import Camera
from kivy.uix.screenmanager import ScreenManager, Screen
output = {}
# Scanner (first)
class Scanner(Screen):
def build(self):
layout = BoxLayout(orientation='vertical')
self.camera = Camera(play=True)
self.camera.texture
layout.add_widget(self.camera)
Clock.schedule_once(self.process, 1)
return layout
def process(self, x):
global output
output = {}
texture = self.camera.texture
frame = np.array(Image.frombytes(mode='RGBA', size=texture.size, data=texture.pixels))
barcodes = decode(frame)
if barcodes:
for barcode in barcodes:
file = csv.reader(open('Book1.csv', 'r', encoding='utf8'), delimiter=',')
line = 0
cols = []
last = [False, -1]
# ...
if len(output[cols[0]]) >= 1:
Clock.schedule_once(self.change)
Clock.schedule_once(self.process)
# Change screen function here! Scanner -> Show
def change(self):
self.manager.transition.direction = 'left'
self.manager.current = 'show'
# Show (second)
class Show(Screen):
def build(self):
layout = BoxLayout(orientation='vertical')
manager = GridLayout(cols=2)
# ...
layout.add_widget(manager)
layout.add_widget(Button(text='Return', on_click=self.change))
# Change screen function here! Show -> Scanner
def change(self, x):
self.manager.transition.direction = 'left'
self.manager.current = 'scanner'
As should be pretty evident, I'm a total beginner at Kivy, so I'd really appreciate it if you could show me exactly what I need to change, including the specific syntax that should be used.
Thanks in advance for your time and wisdom.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
