Kivy ScreenManager错误

时间:2017-06-15 20:42:50

标签: python kivy

我是kivy语言的初学者。我正在写一个应用程序,但我有一些困难。我的应用程序将包含几个屏幕所以我决定使用ScreenManager,但每次我尝试启动.py时我都会收到错误,这会让python没有响应。我已将.py文件和.kv文件保存在同一文件夹中。

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文件:

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'

2 个答案:

答案 0 :(得分:1)

问题在于:

Aladdin

presentation = Builder.load_file("Aladdin.kv") 被解释为变量。如果要将字符串传递给方法,请将其命名为:

{{1}}

答案 1 :(得分:1)

这些文件有几个问题:

  1. @Artur R. Czechowski已经指出缺少引号Builder.load_file('Aladdin.kv')
  2. 您没有在kv文件或python代码中定义根小部件。我通过在构建方法def build(self): return ScreenManager()
  3. 中返回它来改变它
  4. 不是hint_size,正确是size_hint
  5. 我认为您尝试将图像作为背景。这是一种方法,不要忘记文件结束,例如.jpg
  6. canvas.before:
                Rectangle:
                    pos:self.pos
                    size: self.size
                    source:'image1.jpg'
    

    作为旁注:逐步构建应用程序是有意义的。写尽可能少的代码,然后检查。写一些代码,再次检查。快乐编码与kivy :)。

    python代码:

    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()
    

    修订的kv文件:

    <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'