我是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'
答案 0 :(得分:1)
问题在于:
Aladdin
presentation = Builder.load_file("Aladdin.kv")
被解释为变量。如果要将字符串传递给方法,请将其命名为:
{{1}}
答案 1 :(得分:1)
这些文件有几个问题:
Builder.load_file('Aladdin.kv')
def build(self): return ScreenManager()
hint_size
,正确是size_hint
.jpg
canvas.before:
Rectangle:
pos:self.pos
size: self.size
source:'image1.jpg'
作为旁注:逐步构建应用程序是有意义的。写尽可能少的代码,然后检查。写一些代码,再次检查。快乐编码与kivy :)。
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()
<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'