App code:
from kivy.app import App
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.screenmanager import Screen
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import FadeTransition<br/>
kv = '''
ScreenManagement:
transition: FadeTransition()
screen1:
screen2:
<screen1>:
name: 'one'
Button:
text: 'Hello!'
on_release: app.root.current = 'two'
<screen2>:
name: 'two'
Button:
text: 'Hello2'
on_release: app.root.current = 'one'
'''<br/>
class screen1(Screen):
pass
class screen2(Screen):
pass
class ScreenManagement(ScreenManager):
pass
class test23(App):
def build(self):
return Builder.load_string(kv)
<br/>
if __name__ == '__main__':
test23().run()
Every time I execute it I have the following traceback. What's wrong with it?
runfile('/home/pc/python and kivy/test23.py', wdir='/home/pc/python and kivy')<br/>
Traceback (most recent call last):<br/>
File "<stdin>", line 1, in <module><br/>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile<br/>
execfile(filename, namespace)<br/>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile<br/>
builtins.execfile(filename, *where)<br/>
File "/home/pc/python and kivy/test23.py", line 42, in <module><br/>
test23().run()<br/>
File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 802, in run
root = self.build()<br/>
File "/home/pc/python and kivy/test23.py", line 39, in build
return Builder.load_string(kv)<br/>
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1921, in load_string<br/>
self._apply_rule(widget, parser.root, parser.root)<br/>
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 2130, in _apply_rule
e), cause=tb)<br/>
kivy.lang.BuilderException: Parser: File "<inline>", line 3:
..<br/>
1:<br/>
2:ScreenManagement:<br/>
>> 3: transition: FadeTransition()<br/>
4: screen1:<br/>
5: screen2:<br/>
..<br/>
BuilderException: Parser: File "<inline>", line 3:
..<br/>
1:<br/>
2:ScreenManagement:<br/>
>> 3: transition: FadeTransition()<br/>
4: screen1:<br/>
5: screen2:<br/>
..<br/>
NameError: name 'FadeTransition' is not defined<br/>
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1742, in create_handler<br/>
return eval(value, idmap), bound_list<br/>
File "<string>", line 3, in <module><br/><br/>
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 2115, in _apply_rule
rctx['ids'])<br/>
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1747, in create_handler
cause=tb)<br/>
Transitions aren't widgets, nor properties, therefore they aren't imported by default while creating the enviroment for parsing the .kv
file or kv
string, so you'll need to import it at the top of the .kv
file / string.
Example:
#:import FadeTransition kivy.uix.screenmanager.FadeTransition