3

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/>
Peter Badida
  • 11,310
  • 10
  • 44
  • 90
  • This question is very unclear at present and not focussed on a specific issue. Try and explain what you have tried so far to fix the various errors thrown up by the compiler and where exactly you are struggling. – Marcus Oct 29 '16 at 16:19
  • 1
    edit question and use button `{}` to format code. – furas Oct 29 '16 at 16:33
  • @Sparky The question isn't unclear, the author is just confused, because this is specifically framework-related way of handling imports in framework's own language. Error is clear enough though = `NameError: name is not defined`. – Peter Badida Oct 29 '16 at 16:49

2 Answers2

10

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
Peter Badida
  • 11,310
  • 10
  • 44
  • 90
2

At the top of the kv file you need an import. To get the transition it will be

#: import sm kivy.uix.screenmanager

ScreenManager: transition: sm.FadeTransition() screen1: screen2: ....

This is how I do it, and this works for all transitions a full documentation can be found at https://kivy.org/docs/api-kivy.uix.screenmanager.html

I am using python 2.7 and kivy 1.10.0

Robert Holmes
  • 95
  • 1
  • 5