I'm trying to get a handle on Animations. The problem that I'm having is that my Animation plays once but does not repeat.
What I want to do: I want the image to slide up, and then off the screen. Then appear at the bottom of the screen and again slide up and off.
What actually happens: The Image slides "up" the screen, disappears off of the screen and that's it. It doesn't reset back to its original location, and it doesn't repeat.
My question is: How can I reset the position of the image so that it continues in a never-ending cycle (until the "Stop Animation" button is clicked)?
The animated object is the Image that's setup in .kv language it's directly under MyScreen
If you want to see how the animation plays just change the name of the image "binary_rain.png" in kv lang to w/e you have :)
import kivy
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, FallOutTransition
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.actionbar import ActionBar
from kivy.factory import Factory
from kivy.animation import Animation
class MyScreen(Screen):
def __init__(self, **kwargs):
super(MyScreen, self).__init__(**kwargs)
Clock.schedule_interval(self.example_func, 20.0)
self._test_bool = False
self.water = Animation(pos=(0, 800)) # It's setup in __init__ ; here
self.water &= Animation(size=(800, 800), duration=2, t='in_back') # and then here
def example_func(self, dt):
if self._test_bool == True:
self.ids.text_input_test_id.foreground_color = 255,0,0,1
self.ids.text_input_test_id.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_test_id.foreground_color = 0, 255, 0, 1
self.ids.text_input_test_id.text = "The color has changed"
self._test_bool = True
def test_func(self):
if self._test_bool == True:
self.ids.text_input_2.foreground_color = 255,0,0,1
self.ids.text_input_2.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_2.foreground_color = 0, 255, 0, 1
self.ids.text_input_2.text = "The color has changed"
self._test_bool = True
def test_func_two(self):
if self._test_bool == True:
self.ids.text_input_3.foreground_color = 255,0,0,1
self.ids.text_input_3.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_3.foreground_color = 0, 255, 0, 1
self.ids.text_input_3.text = "The color has changed"
self._test_bool = True
def start_my_animation(self): # this is what starts the animation
self.water.repeat = True
self.water.start(self.ids.animated_bacground)
def stop_my_animation(self): # this is what should end the animation
self.water.stop(self.ids.animated_bacground)
class MyScreenManager(ScreenManager):
pass
root_widget = Builder.load_string('''
#:import FallOutTransition kivy.uix.screenmanager.FallOutTransition
#:import Config kivy.config
#:import Window kivy.core.window
#:import Clock kivy.clock
#:import ActionBar kivy.uix.actionbar
#:import Animation kivy.animation.Animation
MyScreenManager:
transition: FallOutTransition()
MyScreen:
<MyScreen>:
name: 'example_screen'
Image:
id: animated_bacground
size: self.size
pos: self.pos
source: 'binary_rain.png'
TextInput:
id: text_input_test_id
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .85}
text: 'Hello'
font: '12sp'
TextInput:
id: text_input_2
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .70}
text: 'Hello'
font: '12sp'
TextInput:
id: text_input_3
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .65}
text: 'Button 3'
Button:
id: test_button_id
size_hint: .3, .05
pos_hint: {'x': .5, 'y': .5}
text: 'Click me'
on_press: root.test_func()
ActionBar:
pos_hint: {'top':1}
ActionView:
use_separator: True
ActionPrevious:
title: 'Menu'
with_previous: False
ActionOverflow:
ActionButton:
text: 'click me'
on_press: root.test_func_two()
ActionButton:
text: 'Start Animation'
on_press: root.start_my_animation()
ActionButton:
text: 'Stop Animation'
on_press: root.stop_my_animation()
ActionButton:
text: 'Button 2'
ActionButton:
text: 'Button 3'
ActionGroup:
text: 'Group1'
ActionButton:
text: 'Button 5'
ActionButton:
text: 'Btn6'
ActionButton:
text: 'Btn7'
''')
Factory.unregister('ActionPrevious')
class TestApp(App):
def build(self):
self.title = 'Example App'
return root_widget
TestApp().run()
Your animation is setting the size, not the pos...
also you are doing both animations in the same time using &=, you need +=
self.water = Animation(pos=(0, 800))
# notice the changes * pos * and * += *
self.water += Animation(pos=(800, 800), duration=2, t='in_back') # and then here
Related
I designed this program
python code:
from random import random
from kivymd.app import MDApp as App
from kivymd.uix.widget import MDWidget as Widget
from kivy.graphics import Ellipse, Color, Line
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.button import MDIconButton
from kivy.lang import Builder
from kivy.properties import ObjectProperty
class Main(Screen):
draw = ObjectProperty(None)
class About(Screen):
pass
class Manager(ScreenManager):
mgr = ObjectProperty(None)
def cleaner(self):
self.mgr.draw.clean()
def color(self,color):
MyPaintWidget.color = color
class MyPaintWidget(Widget):
color = (0,0,0,1)
def clean(self):
self.canvas.clear()
def on_touch_down(self, touch):
# self.rect.pos = touch.pos
color = self.color
with self.canvas:
Color(*color)
d = 10
touch.ud['line'] = Line(points=(touch.x, touch.y),width = 5)
def on_touch_up(self,touch):
with self.canvas:
d = 10
Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
def on_touch_move(self, touch):
# self.rect.pos = touch.pos
touch.ud['line'].points += [touch.x, touch.y]
#main_style = Builder.load_file("ms_paint.kv")
class MyPaintApp(App):
def build(self):
self.root = Builder.load_file("ms_paint.kv")
if __name__ == '__main__':
MyPaintApp().run()
kv file:
#:import get_color_from_hex kivy.utils.get_color_from_hex
Manager:
mgr: sc_mgr
Main:
id: sc_mgr
About:
<MyPaintWidget>:
on_touch_down: self.on_touch_down
on_touch_move: self.on_touch_move
<Main>:
name: "main"
draw: main_draw
BoxLayout:
orientation: "vertical"
MyPaintWidget:
id: main_draw
size_hint: 1, 0.9
GridLayout:
size_hint: 1, 0.1
cols: 7
padding: 10
#Button:
#text: "About"
#on_press:
#root.manager.current= "about"
#root.manager.transition.direction= "left"
MDIconButton:
icon: "eraser-variant"
on_press: root.manager.cleaner()
MDIconButton:
icon: "circle"
theme_icon_color: "Custom"
icon_color: (0,0,0,1)
on_press: root.manager.color((0,0,0,1))
MDIconButton:
icon: "circle"
theme_icon_color: "Custom"
icon_color: get_color_from_hex("#ff0000")
on_press: root.manager.color((255,0,0,1))
MDIconButton:
icon: "circle"
theme_icon_color: "Custom"
icon_color: get_color_from_hex("#00ff00")
on_press: root.manager.color((0,255,0,1))
MDIconButton:
icon: "circle"
theme_icon_color: "Custom"
icon_color: get_color_from_hex("#0000ff")
on_press: root.manager.color((0,0,255,1))
<About>:
name: "about"
BoxLayout:
orientation: "vertical"
padding: 10
Label:
size_hint: 1, 0.9
text: "This is a free and open source paint app, made by a teacher of HamRuyesh community."
Button:
size_hint: 1, 0.9
text:"Back to Main"
on_press:
root.manager.current= "main"
root.manager.transition.direction= "right"
My problem is that I do not want to draw a line in the bottom bar, which is a grid layout, and I just want to be able to draw from that part. please guide me.
..........................................................................................................................................
You can use StencilView to limit the drawing area.
Thus You might change the MyPaintWidget(Widget) as
class MyPaintWidget(BoxLayout, StencilView):
I'm making a game in kivy, it's a soccer juggling game. I want the game to be over whenever the soccer ball falls off the screen, also I want a restart button when the game is over so I can play again. I tried a lot of things but it doesn't seem to work. Any help is appreciated. Below is my code! Thank You!
main.py
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.image import Image
from kivy.core.audio import SoundLoader
from kivy.clock import Clock
from kivy.properties import NumericProperty
from kivy.vector import Vector
class HomeScreen(Screen):
pass
def play_sound(self):
sound = SoundLoader.load('button press sound.wav.')
if sound:
sound.play()
sound = SoundLoader.load('Crowd sound effect.wav')
sound.loop = True
sound.play()
class GameScreen(Screen):
pass
def play_sound(self):
sound = SoundLoader.load('button press sound.wav.')
if sound:
sound.play()
class Ball(Image):
velocity = NumericProperty(0)
def on_touch_down(self, touch):
if Vector(self.center).distance(touch.pos) <= 33:
label = App.get_running_app().root.get_screen('game_screen').ids.score
label.text = str(int(label.text) + 1)
sound = SoundLoader.load('Soccer ball sound.wav')
sound.play()
self.source = "icons/ball.png"
self.velocity = 275
return super().on_touch_down(touch)
def on_touch_up(self, touch):
if Vector(self.center).distance(touch.pos) <= 33:
self.source = "icons/ball.png"
return super().on_touch_up(touch)
class MainApp(App):
GRAVITY = 300
def move_ball(self, time_passed):
ball = self.root.ids.game_screen.ids.ball
ball.y = ball.y + ball.velocity * time_passed
ball.velocity = ball.velocity - self.GRAVITY * time_passed
self.check_collision()
def check_collision(self):
ball = self.root.ids.game_screen.ids.ball
if ball.top < 96:
self.game_over()
def game_over(self):
print("game over")
def start_game(self):
Clock.schedule_interval(self.move_ball, 1/60.)
self.root.ids.game_screen.ids.score.text = "0"
def change_screen(self, screen_name):
self.root.current = screen_name
MainApp().run()
homescreen.kv
#:import utils kivy.utils
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
<HomeScreen>:
FloatLayout:
canvas:
Color:
rgb: utils.get_color_from_hex("#39B3F2")
Rectangle:
size: self.size
pos: self.pos
GridLayout:
rows: 1
pos_hint: {"top": 1, "left": 1}
size_hint: 1, .9
Image:
source: "icons/keepyup.png"
FloatLayout:
Button:
font_size: dp(20)
font_name: 'SackersGothicStd-Medium.otf'
text: "PLAY"
color: "gold"
pos_hint: { "center_x": .5, "center_y": .3}
size: 80, 55
size_hint: None, None
background_normal: ''
background_color: (57/255.0, 179/255.0, 242/255.0, .10)
on_press:
on_release:
root.play_sound()
root.manager.transition = FadeTransition()
app.change_screen("game_screen")
gamescreen.kv
#:import utils kivy.utils
<GameScreen>:
FloatLayout:
canvas:
Color:
rgb: utils.get_color_from_hex("#39B3F2")
Rectangle:
size: self.size
pos: self.pos
GridLayout:
rows: 1
pos_hint: {"top": 1, "left": 1}
size_hint: 1, .1
Image:
source: "icons/sun.png"
GridLayout:
rows: 1
pos_hint: {"top": 1, "left": 1}
size_hint: 1, .2
Image:
source: "icons/clouds.png"
GridLayout:
rows: 1
pos_hint: {"bottom": 1, "left": 1}
size_hint: 1, .5
Image:
source: "icons/Field4.png"
allow_stretch: True
keep_ratio: False
pos: self.pos
Label:
id: score
size_hint: None, None
font_size: dp(25)
font_name: 'SackersGothicStd-Medium.otf'
text: "0"
color: "gold"
pos_hint: { "center_x": 0.1, "center_y": 0.9}
Button:
size_hint: None, None
font_size: dp(20)
font_name: 'SackersGothicStd-Medium.otf'
text: "Start Game"
color: "gold"
pos_hint: { "center_x": 0.5, "center_y": 0.3}
size: 150, 55
size_hint: None, None
background_normal: ''
background_color: (57/255.0, 179/255.0, 242/255.0, .10)
on_release:
self.disabled = True
self.opacity = 0
root.play_sound()
app.start_game()
Ball:
source: "icons/ball.png"
size_hint: None, None
size: 500, 500
pos_hint: {"center_x": 0.5}
id: ball
main.kv
#:include kv/homescreen.kv
#:include kv/gamescreen.kv
ScreenManager:
id: screen_manager
HomeScreen:
name: "home_screen"
id: home_screen
GameScreen:
name: "game_screen"
id: game_screen
Firstly, define a name for clock so you can stop it when game over:
self.control = Clock.schedule_interval(self.move_ball, 1/60.)
In game_over func, let's stop this clock:
self.control.cancel()
Now we need to create BoxLayout for our popup: ( game_over function )
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
bx = BoxLayout(orientation='vertical')
restart_but = Button(text='Restart')
restart_but.bind(on_release=self.restart_game)
bx.add_widget(Label(text='Score :'+self.root.ids.game_screen.ids.score.text))
bx.add_widget(restart_but)
self.popup = Popup(title='Game Over',content=bx,size_hint=(None, None), size=(400, 400))
self.popup.open()
Now as we define release command to restart_but, need to create new function:
def restart_game(self,*args):
self.popup.dismiss() #Close popup
#default settings here..
self.control = Clock.schedule_interval(self.move_ball, 1/60.) #Start Game
In restart_game function, you need to reset all settings like ball's position and score.
Also I suggest you to use KivyMD for sightly popups and buttons.
I am building an OCR application for visually impaired users. I'm wanting the app to open straight away onto the camera screen, when the user takes a pic on button press I want ocr process to occur and display the output on the text screen in a lbl or txtbox and have a TTS read out what the text says. My issue is that i am having trouble obtaining the output of ocr and displaying it, I'm not familiar with screenmanager or python. ideally the opencv and tesseract process would occur in same function as the capture however i cant get the output recognised on the following screen. heres some code, any suggestions and help appreciated!
# Importing the libraries
import cv2
import pytesseract
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
pytesseract.pytesseract.tesseract_cmd = r'D:/pytesseract/tesseract.exe'
# voice_text = ""
# for i in ocrtext.split():
# voice_text += i + ' '
# voice_text = voice_text[:-1]
# voice_text
# engine = pyttsx3.init()
# engine.setProperty("rate", 145)
# voices = engine.getProperty('voices')
# engine.setProperty('voice', voices[0].id)
# engine.say(voice_text)
# engine.runAndWait()
class CameraScreen(Screen):
def capture(self):
camera = self.ids['camera']
camera.export_to_png("./picforocr.png")
image = cv2.imread("./picforocr.png")
ocrtext = pytesseract.image_to_string(image)
class TextScreen(Screen):
pass
GUI = Builder.load_string("""
GridLayout:
cols: 1
ScreenManager:
id: screen_manager
CameraScreen:
name: "camera_screen"
id: camera_screen
TextScreen:
name: "text_screen"
id: text_screen
<CameraScreen>:
orientation: 'vertical'
GridLayout:
cols: 1
Camera:
id: camera
resolution: (800, 800)
Button:
text: 'OCR!'
size_hint_y: None
height: '48dp'
on_press:
root.capture()
# root refers to <CameraScreen>
# app refers to TestCamera, app.root refers to the GridLayout: at the top
app.root.ids['screen_manager'].transition.direction = 'left'
app.root.ids['screen_manager'].current = 'text_screen'
<TextScreen>:
Label:
id: ocr_output
text:
Camerascreen.ocrtext
font_size: 20
Button:
text: "Do OCR Again"
size_hint_y: None
height: '48dp'
font_size: 50
on_press:
app.root.ids['screen_manager'].transition.direction = 'right'
app.root.ids['screen_manager'].current = 'camera_screen'
""")
class MyOCRApp(App):
def build(self):
return GUI
if __name__ == "__main__":
MyOCRApp().run()
This is basic code for passing texts from one class to another and also TextInput data. This is python code:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
class FirstPage(FloatLayout):
def switch_screen(self):
secondpage = self.login_name = self.ids.login_name.text
myapp.secondpage.update_name1(secondpage)
secondpage = self.password_name = self.ids.password_name.text
myapp.secondpage.update_name2(secondpage)
myapp.screen_manager.transition = SlideTransition(direction='left', duration=.25)
myapp.screen_manager.current = 'SecondPage'
class SecondPage(FloatLayout):
def switch_back(self):
myapp.screen_manager.transition = SlideTransition(direction='right', duration=.25)
myapp.screen_manager.current = 'FirstPage'
def update_name1(self, name_login):
self.ids.name_login.text = (f'{name_login}:')
def update_name2(self, name_password):
self.ids.name_password.text = (f'{name_password}:')
class MyApp(App):
def build(self):
self.screen_manager = ScreenManager()
self.firstpage = FirstPage()
screen = Screen(name='FirstPage')
screen.add_widget(self.firstpage)
self.screen_manager.add_widget(screen)
self.secondpage = SecondPage()
screen = Screen(name='SecondPage')
screen.add_widget(self.secondpage)
self.screen_manager.add_widget(screen)
return self.screen_manager
myapp = MyApp()
myapp.run()
And this is kivy code:
FirstPage:
<FirstPage>:
TextInput:
id: login_name
size_hint: .65, .08
multiline: False
font_size: 20
pos_hint: {'x': .2, 'y': .57}
TextInput:
id: password_name
size_hint: .65, .08
multiline: False
font_size: 20
pos_hint: {'x': .2, 'y': .47}
Button:
text: "First"
size_hint: .1, .1
on_release: root.switch_screen()
<SecondPage>:
Button:
text: "Second"
size_hint: .1, .1
on_release: root.switch_back()
Label:
id: name_login
text: "Login"
size_hint: .2, .2
pos_hint: {'x': .2, 'y': .57}
Label:
id: name_password
text: "Password"
size_hint: .2, .2
pos_hint: {'x': .2, 'y': .47}
I'm new to KIVYMD and have been trying to play around by creating a log in screen that transfers the user to a new page. I've gotten making the core of the screen down, like adding the textfields and buttons, but am not sure how I can implement a screen changing functionality to my code. Any help is greatly appreciated.
For reference here is my main code block:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from helpers import username_helper,password_helper
from kivy.uix.screenmanager import Screen
from kivymd.uix.button import MDRectangleFlatButton
class Demo_app(MDApp):
def build(self):
screen = Screen()
self.theme_cls.primary_palette= "Pink"
self.username= Builder.load_string(username_helper)
screen.add_widget(self.username)
self.password = Builder.load_string(password_helper)
screen.add_widget(self.password)
login = MDRectangleFlatButton(text="Log In", pos_hint={"center_x": 0.5, "center_y":0.4})
screen.add_widget(login)
signin = MDRectangleFlatButton(text="Sign Up", pos_hint={"center_x": 0.5, "center_y": 0.3})
screen.add_widget(signin)
return screen
Demo_app().run()
And here is my .kv file:
username_helper="""
MDTextField:
hint_text: "Create/Enter your Username"
helper_text:"Or click here if you forgot your Username"
helper_text_mode: "on_focus"
icon_right: "language-python"
icon_right_color: app.theme_cls.primary_color
pos_hint:{"center_x":0.5,"center_y":0.6}
size_hint_x:None
width:300
"""
password_helper="""
MDTextField:
hint_text: "Create/Enter your Password"
helper_text:"Or click here if you forgot your password "
helper_text_mode: "on_focus"
icon_right: "language-python"
icon_right_color: app.theme_cls.primary_color
pos_hint:{"center_x":0.5,"center_y":0.5}
size_hint_x:None
width:300
"""
I think you want to transfer those textinput data to a label in different class. I think this is what you need.(keep an eye on ids):
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
class FirstPage(FloatLayout):
def switch_screen(self):
secondpage = self.login_name = self.ids.login_name.text
myapp.secondpage.update_name1(secondpage)
secondpage = self.password_name = self.ids.password_name.text
myapp.secondpage.update_name2(secondpage)
myapp.screen_manager.transition = SlideTransition(direction='left', duration=.25)
myapp.screen_manager.current = 'SecondPage'
class SecondPage(FloatLayout):
def switch_back(self):
myapp.screen_manager.transition = SlideTransition(direction='right', duration=.25)
myapp.screen_manager.current = 'FirstPage'
def update_name1(self, name_login):
self.ids.name_login.text = (f'{name_login}:')
def update_name2(self, name_password):
self.ids.name_password.text = (f'{name_password}:')
class MyApp(App):
def build(self):
self.screen_manager = ScreenManager()
self.firstpage = FirstPage()
screen = Screen(name='FirstPage')
screen.add_widget(self.firstpage)
self.screen_manager.add_widget(screen)
self.secondpage = SecondPage()
screen = Screen(name='SecondPage')
screen.add_widget(self.secondpage)
self.screen_manager.add_widget(screen)
return self.screen_manager
myapp = MyApp()
myapp.run()
Here's kivy code:
FirstPage:
<FirstPage>:
TextInput:
id: login_name
size_hint: .65, .08
multiline: False
font_size: 20
pos_hint: {'x': .2, 'y': .57}
TextInput:
id: password_name
size_hint: .65, .08
multiline: False
font_size: 20
pos_hint: {'x': .2, 'y': .47}
Button:
text: "First"
size_hint: .1, .1
on_release: root.switch_screen()
<SecondPage>:
Button:
text: "Second"
size_hint: .1, .1
on_release: root.switch_back()
Label:
id: name_login
text: "Login"
size_hint: .2, .2
pos_hint: {'x': .2, 'y': .57}
Label:
id: name_password
text: "Password"
size_hint: .2, .2
pos_hint: {'x': .2, 'y': .47}
Here's the relevant .py code:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.core.image import Image
from kivy.graphics import Color, Rectangle
from kivy.core.audio import SoundLoader
from kivy.core.text import LabelBase
import pygame
from pygame.locals import*
import random
class RootScreen(ScreenManager):
pass
class LevelOneBedroomScreen(Screen):
def __init__(self, **kwargs):
super(LevelOneBedroomScreen, self).__init__(**kwargs)
def next_text(self):
if self.ids.level_one_bedroom_text.text == '*YAWN*':
self.ids.level_one_bedroom_image.source = 'placeholder_background2.png'
self.ids.level_one_bedroom_text.text = 'Hello'
elif self.ids.level_one_bedroom_text.text == 'Hello':
self.manager.current = 'level one bedroom choice'
class LevelOneBedroomChoiceScreen(Screen):
def __init__(self, **kwargs):
super(LevelOneBedroomChoiceScreen, self).__init__(**kwargs)
def get_image(self):
#This part is not working
self.ids.level_one_bedroom_choice_image.source = LevelOneBedroomScreen.ids.level_one_bedroom_image.source
class screensApp(App):
def build(self):
return RootScreen()
if __name__ == "__main__":
screensApp().run()
Near the bottom I commented the part of the code that does not work. I have two, nearly identical screens where one LevelOneBedroomScreen has text and LevelOneBedroomChoiceScreen has buttons. I need both background images to be the same, and since the first screen updates images occasionally I wrote something in the .kv file to hopefully pull the image from the first screen but it results in an error. Here is the relevant code from the .kv:
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
<RootScreen>:
transition: FadeTransition()
LevelOneBedroomScreen:
LevelOneBedroomChoiceScreen:
<LevelOneBedroomScreen>:
name: 'level one bedroom'
id: level_one_bedroom
Image:
id: level_one_bedroom_image
source: 'placeholder_background.png'
size: self.size
pos: self.pos
allow_stretch: True
keep_ratio: False
BoxLayout:
orientation: 'vertical'
Label:
text: 'Image of Bedroom'
Button:
text: 'Dialogue'
background_color: 0,0,0,0
BoxLayout:
size_hint_y: .2
Label:
text: 'left arrow'
Button:
text: 'choose'
on_release: root.manager.current = 'level one bedroom choice'
Label:
text: 'right arrow'
<LevelOneBedroomChoiceScreen>:
name: 'level one bedroom choice'
id: level_one_bedroom_choice
Image:
id: level_one_bedroom_choice_image
source: get_image()
size: self.size
pos: self.pos
allow_stretch: True
keep_ratio: False
BoxLayout:
orientation: 'vertical'
Label:
text: 'Image of Bedroom'
BoxLayout:
orientation: 'vertical'
BoxLayout:
Button:
text: 'choice 1'
Button:
text: 'choice 2'
BoxLayout:
Button:
text: 'choice 3'
Button:
text: 'choice 4'
BoxLayout:
size_hint_y: .2
Label:
text: 'left arrow'
Button:
text: 'home'
on_release: root.manager.current = 'levels'
Label:
text: 'right arrow'
I inserted a comment showing which part of the code results in error. Thank you in advance, greatly appreciate your feedback.
Using Kivy Screen Manager, replace LevelOneBedroomScreen.ids.level_one_bedroom_image.source with self.manager.ids.level_one_bedroom.ids.level_one_bedroom_image.source
Example
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
class Home(ScreenManager):
pass
class LevelOneBedroomScreen(Screen):
def __init__(self, **kwargs):
super(LevelOneBedroomScreen, self).__init__(**kwargs)
def next_text(self):
if self.ids.level_one_bedroom_text.text == '*YAWN*':
self.ids.level_one_bedroom_image.source = 'placeholder_background2.png'
self.ids.level_one_bedroom_text.text = 'Hello'
elif self.ids.level_one_bedroom_text.text == 'Hello':
self.manager.current = 'level one bedroom choice'
class LevelOneBedroomChoiceScreen(Screen):
def __init__(self, **kwargs):
super(LevelOneBedroomChoiceScreen, self).__init__(**kwargs)
def on_pre_enter(self, *args):
self.ids.level_one_bedroom_choice_image.source = self.manager.ids.level_one_bedroom.ids.level_one_bedroom_image.source
class TestApp(App):
def build(self):
return Home()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.9.1
<Home>:
LevelOneBedroomScreen:
id: level_one_bedroom
LevelOneBedroomChoiceScreen:
id: level_one_bedroom_choice
<LevelOneBedroomScreen>:
name: 'level one bedroom'
BoxLayout:
orientation: 'vertical'
Image:
id: level_one_bedroom_image
source: 'bedroom.jpeg' # 'placeholder_background.png'
size: self.size
pos: self.pos
allow_stretch: True
keep_ratio: False
Button:
size_hint: 1, 0.2
text: 'Bedroom Choice'
on_release: root.manager.current = 'level one bedroom choice'
<LevelOneBedroomChoiceScreen>:
name: 'level one bedroom choice'
Image:
id: level_one_bedroom_choice_image
#below is the code that fails
# source:
size: self.size
pos: self.pos
allow_stretch: True
keep_ratio: False
Output