0

I wanted to make a kivy program with two screens. On the first screen there is only one button allowing you to go to the next screen. On thesecond screen there are two buttons, one for going back to the first screen, and the other one is supposed to display an image on the whole screen. I did this by making a function, that creates an image and adds it to the Box Layout. I know, that the Image would not be displayed fullscreen, but I did not know a better solution. But when I clicked the button, the whole program crashed. Here is the source code:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image

class Manager(ScreenManager):
    pass
class FirstScreen(Screen):

    def change_text(self):

        self.manager.current = 'second'
class SecondScreen(Screen):
    def display_image(self):
        bild = Image(source='Bild1.png')
        box1.add_widget(bild)
root_widget = Builder.load_string('''
Manager:
    FirstScreen:
    SecondScreen:
<FirstScreen>:
    name: 'first'
    BoxLayout:
        orientation: 'vertical'
        Button:
            id: b1
            text: 'Go to next Screen'
            on_release: root.change_text()
<SecondScreen>:
    name: 'second'
    BoxLayout:
        id: box1
        orientation: 'vertical'

        Button:
            id: b2
            text: 'Go back'
            on_release: app.root.current = 'first'
        Button:
            id: b3
            text: 'suprise'
            on_release: root.display_image()
''')
class Caption(App):
    def build(self):
        return root_widget

Caption().run()

And here is the error message:

File "<string>", line 26, in <module>
File "img.py", line 18, in display_image
   box1.add_widget(bild)
NameError: global name 'box1' is not defined
2
  • Can you give us a traceback? Jan 20, 2015 at 14:33
  • Ok, here is the error massage
    – Gilgamesch
    Jan 20, 2015 at 15:29

1 Answer 1

2

In order to use ids you need to access them via the ids object on the root rule widget. So instead of box1.add_widget(bild) you should use self.ids.box1.add_widget(bild).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.