[英]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. 我的应用程序将由几个屏幕组成,所以我决定使用ScreenManager,但是每次尝试启动.py时,都会出现错误,这会使python没有响应。 I have saved both the .py file and the .kv file in the same folder. 我已经将.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 file: 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'
The problem is in the line: 问题出在行中:
presentation = Builder.load_file(Aladdin.kv)
Aladdin
is interpreted as the variable. Aladdin
被解释为变量。 If you want to pass a string to the method, call it like: 如果要将字符串传递给方法,请按以下方式调用它:
presentation = Builder.load_file("Aladdin.kv")
The files had a couple of issues: 这些文件有两个问题:
Builder.load_file('Aladdin.kv')
@Artur R. Czechowski已经指出缺少引号Builder.load_file('Aladdin.kv')
def build(self): return ScreenManager()
我通过在构建方法def build(self): return ScreenManager()
返回它来更改了它def build(self): return ScreenManager()
hint_size
, correct is size_hint
它不是hint_size
,正确的是size_hint
.jpg
这是一种方法,请不要忘记以.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 :). 与基维快乐编码:)。
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.