aladdin hammodi
aladdin hammodi

Reputation: 21

Kivy ScreenManager Errors

I'm a beginner with kivy language. I'm writing an app but I have some difficulties. My app is going to consist of a few screens so I decided to use the ScreenManager but each time I'm trying to launch the .py I get an error and this makes the python not responding. I have saved both the .py file and the .kv file in the same folder.

Traceback (most recent call last):
   File "C:\Users\Eng. Aladdin Hammodi\Desktop\kivy\main.py", line 15, in <module>
     presentation = Builder.load_file(Aladdin.kv)
 NameError: name 'Aladdin' is not defined

Python file:

import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.lang import Builder

presentation = Builder.load_file(Aladdin.kv)


class ScreenOne(Screen):
    pass


class ScreenTwo(Screen):
    pass

class ScreenManager(ScreenManager):
    pass


class AladdinApp(App):
    def build(self):
        return presentation

sample_app = AladdinApp()
sample_app.run()

aladdin.kv

<ScreenOne>:
    name:screen1

    FloatLayout:
        canvas:
            source:'image1'
        Label:
            text:'Hello\n Welcome to my app\n'
            font_size: 40

        Button:
            text: 'Next'
            pos: 0,1
            font_size:20
            hint_size:0.1,0.05
            on_press:root.manager.current='screen2'
<ScreenTwo>:
    name:screen2
    FloatLayout:
       canvas:
            source:'image1'
        Label:
            text:'Please insert your name'
            text:'Please insert your Password'
            font_size: 40

        Button:
            text: 'Next'
            pos: 0,1
            font_size:20
            hint_size:0.1,0.05
            on_press:root.manager.current='screen1'

Upvotes: 0

Views: 714

Answers (2)

PalimPalim
PalimPalim

Reputation: 3048

The files had a couple of issues:

  1. @Artur R. Czechowski already pointed out missing quotation marks Builder.load_file('Aladdin.kv')
  2. you did not define a root widget in the kv file or in your python code. I changed this by returning it in the build method def build(self): return ScreenManager()
  3. it is not hint_size, correct is size_hint
  4. I think you tried having an image as background. This is one way of doing it, do not forget the file ending e.g. .jpg

canvas.before:
            Rectangle:
                pos:self.pos
                size: self.size
                source:'image1.jpg'

As a side note: It makes sense to build your app step by step. Write as little code as possible which works and then check. Write some more code, check again. Happy coding with kivy :).

python code:

import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.lang import Builder

#### in comment requested to also make cursor visible and not full screen ####
from kivy.config import Config 
Config.set('graphics', 'fullscreen', '0')
Config.set('graphics','show_cursor','1')
####

Builder.load_file('Aladdin.kv')


class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    pass

class ScreenManager(ScreenManager):
    pass


class AladdinApp(App):
    def build(self):
        return ScreenManager()

AladdinApp().run()

revised kv file:

<ScreenManager>:
    ScreenOne:
    ScreenTwo:

<ScreenOne>:
    name:'screen1'

    FloatLayout:
        canvas.before:
            Rectangle:
                pos:self.pos
                size: self.size
                source:'image1.jpg'
        Label:
            text:'Hello\n Welcome to my app\n'
            font_size: 40

        Button:
            text: 'Next'
            pos: 0,1
            font_size:20
            size_hint:0.1,0.05
            on_press:root.manager.current='screen2'
<ScreenTwo>:
    name:'screen2'
    FloatLayout:
        canvas.before:
            Rectangle:
                pos:self.pos
                size: self.size
                source:'image1.jpg'
        Label:
            text:'Please insert your name'
            #text:'Please insert your Password'
            font_size: 40

        Button:
            text: 'Next'
            pos: 0,1
            font_size:20
            size_hint:0.1,0.05
            on_press:root.manager.current='screen1'

Upvotes: 1

ArturFH
ArturFH

Reputation: 1787

The problem is in the line:

presentation = Builder.load_file(Aladdin.kv)

Aladdin is interpreted as the variable. If you want to pass a string to the method, call it like:

presentation = Builder.load_file("Aladdin.kv")

Upvotes: 1

Related Questions