I'm currently trying to get a python variable (called prior) into kivy and dynamicaly change the text of a label when I do. I'm still new to kivy and I'm not sure why I'm getting this error kivy.lang.parser.ParserException: Parser: File "/Users/sam/Downloads/App/answer.kv", line 35:
...
33: color: 0,1,0,1
34: font_size: 75
>> 35: if app.prior == "yes":
36: print("Hello")
37: text: "Correct"
...
Invalid class name
Here is what I currently have Python:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.image import Image
import random
import os
from kivy.core.window import Window
import numpy as np
from kivy.properties import StringProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
class MyLayout(Widget):
pass
class RootLayout(FloatLayout):
pass
class colorApp(App):
flagfile = StringProperty("0")
path = "/Users/sam/Downloads/App/w320"
files = os.listdir(path)
flagfile= path + "/" + random.choice(files)
prior = StringProperty()
def button_press(self):
self.prior = self.root.ids.user_input.text
return RootLayout()
def build(self):
return RootLayout()
if __name__ == "__main__":
colorApp().run()
Kivy
<RootLayout>
MyLayout:
BoxLayout:
orientation: "vertical"
size: root.width, root.height
padding: 50
spacing: 20
Image:
id: cardimage
#source: app.flagfile
source: "w320/in.png"
TextInput:
multiline: False
text: "Enter Guess Here"
on_focus: self.text = "" if args[1] else self.text
id: user_input
Button:
text: "Submit"
font_size: 32
on_press: app.button_press()
FloatLayout:
Label:
text:''
pos: 1200, 910
color: 0,1,0,1
font_size: 75
if app.prior == "yes":
print("Hello")
text: "Correct"
In your kv
, the unindentation of the if app.prior == "yes":
line makes kivy parser expect a new child of the FloatLayout
instance. When that line is interpreted as a class, you see that error. Since you are trying to set the text
property of the Label
, that logic should be used in the text
property definition. Here is one way to do that:
Label:
pos: 1200, 910
color: 0,1,0,1
font_size: 75
text: "Correct" if app.prior == "yes" else ''