Reputation: 13
How can I change the color/image of the second screen based on the button I press using both Kivy and Python? Here is the code that illustrates my problem.
Python:
startvalue = 0
class Start(Screen):
pass
class End(Screen):
global startvalue
def which_color(self):
if startvalue == 1:
self.bg_image.source = 'blue.png'
elif startvalue == 2:
self.bg_image.source = 'red.png'
elif startvalue == 3:
self.bg_image.source = 'yellow.jpg'
elif startvalue == 4:
self.bg_image.source = 'green.jpg'
Kivy:
#: import startvalue __kivyseeing__.startvalue
ScreenManagement:
Start:
End:
<Start>:
startvalue: startvalue
id: start
name: 'start'
BoxLayout:
Button:
name: 'btn1'
id: btn1
text: "btn1"
on_release:
startvalue = 1
app.root.current = 'end'
Button:
name: 'btn2'
id: btn2
text: "btn2"
on_release:
startvalue = 2
app.root.current = 'end'
Button:
name: 'btn3'
id: btn3
text: "btn3"
on_release:
startvalue = 3
app.root.current = 'end'
Button:
name: 'btn4'
id: btn4
text: "btn4"
on_release:
startvalue = 4
app.root.current = 'end'
<End>:
id: end
name: "end"
endbtn: endbtn
bg_image: bg_image
on_pre_enter: self.which_color()
Image:
id: bg_image
source: ""
allow_stretch: True
Button:
name: 'endbtn'
id: endbtn
text: "end"
size_hint_y: .35
size_hint_x: .25
on_release: app.root.current = 'start'
I realize that globals can be pretty dangerous and I'd rather not use them but I don't see how else it can work. Also, I'd like to know how to change Python globals in Kivy and then use them in the second class in order to determine the screen background.
Upvotes: 0
Views: 348
Reputation: 16041
The solution is to use Kivy NumericProperty and hook it up. Please refer to the example below for details.
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty
class ScreenManagement(ScreenManager):
pass
class Start(Screen):
startvalue = NumericProperty(0)
class End(Screen):
def which_color(self, startvalue):
print("startvalue={}".format(startvalue))
if startvalue == 1:
self.bg_image.source = 'blue.png'
elif startvalue == 2:
self.bg_image.source = 'red.png'
elif startvalue == 3:
self.bg_image.source = 'yellow.jpg'
elif startvalue == 4:
self.bg_image.source = 'green.jpg'
class TestApp(App):
def build(self):
return ScreenManagement()
if __name__ == "__main__":
TestApp().run()
#:kivy 1.10.0
<ScreenManagement>:
Start:
id: start
End:
startvalue: start.startvalue
<Start>:
name: 'start'
BoxLayout:
Button:
name: 'btn1'
id: btn1
text: "btn1"
on_release:
root.startvalue = 1
app.root.current = 'end'
Button:
name: 'btn2'
id: btn2
text: "btn2"
on_release:
root.startvalue = 2
app.root.current = 'end'
Button:
name: 'btn3'
id: btn3
text: "btn3"
on_release:
root.startvalue = 3
app.root.current = 'end'
Button:
name: 'btn4'
id: btn4
text: "btn4"
on_release:
root.startvalue = 4
app.root.current = 'end'
<End>:
id: end
name: "end"
endbtn: endbtn
bg_image: bg_image
on_pre_enter: self.which_color(root.startvalue)
Image:
id: bg_image
source: ""
allow_stretch: True
Button:
name: 'endbtn'
id: endbtn
text: "end"
size_hint_y: .35
size_hint_x: .25
on_release: app.root.current = 'start'
Upvotes: 1
Reputation: 8041
You try to use the value from the first screen:
<End>:
id: end
name: "end"
endbtn: endbtn
bg_image: bg_image
on_pre_enter:
self.which_color(self.manager.get_screen('start').startvalue)
And in the python file:
def which_color(self, startvalue):
...
Upvotes: 0