我实际上正在使用kivy lib编写应用程序,我的官方代码出现问题所以我用一个简单的kivy文件(debug.kv)创建了一个小的调试代码(debug.py)。当我启动应用程序时,它会给我一个黑屏。为什么?感谢。
debug.py:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class App(App):
def build(self):
return Screen1()
class Screen1(BoxLayout):
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)
def change_screen(self):
return Screen2()
class Screen2(BoxLayout):
def __init__(self, **kwargs):
super(Screen2, self).__init__(**kwargs)
def change_screen(self):
return Screen1()
if __name__ == '__main__':
App().run()
debug.kv:
#:kivy 1.9.1
<Screen1>
Label:
text:"Screen 1"
Button:
text:"Go to screen 2"
size:(100,300)
size_hint:(None,None)
on_press:root.change_screen()
<Screen2>
Label:
text:"Screen 2"
Button:
text:"Go to screen 1"
size:(100,300)
size_hint:(None,None)
on_press:root.change_screen()
____编辑____
如何从python代码更改屏幕? 因为在我的官方代码中,我希望有一个功能,只有在密码正确的情况下才能检查密码并更改屏幕。
示例(虽然不起作用,但您可以理解我的想法):
的.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
class MyScreenManager(ScreenManager):
pass
class Screen1(Screen):
def goto2(self):
root.manager.current = "screen2"
pass
class Screen2(Screen):
def say_hello(self):
print 'hello2'
pass
class DebugApp(App):
def build(self):
return MyScreenManager()
if __name__ == '__main__':
DebugApp().run()
.kv
#:kivy 1.9.1
<MyScreenManager>:
Screen1:
Screen2:
<Screen1>:
name: "screen1"
BoxLayout:
Label:
text:"Screen 1"
Button:
text: "Go to screen 2"
size: (300,300)
size_hint:(None,None)
color:(1,0,0,1)
on_press: root.goto2()
<Screen2>:
name: "screen2"
BoxLayout:
Label:
text: "Screen 2"
Button:
text: "Go to screen 1"
size: (300,300)
size_hint: (None,None)
color:(1,0,0,1)
on_press: root.manager.current = "screen1"
答案 0 :(得分:0)
你得到一个黑屏,因为Kivy无法提交Kivy规则文件。请更改您的班级&#34;应用程序(应用程序):&#34; to&#34; DebugApp(App):&#34;
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
class MyScreenManager(ScreenManager):
pass
class LoginScreen(Screen):
def check_password(self, instance, password):
if password == "pwd":
instance.current = "screen2"
class Screen2(Screen):
pass
class TestApp(App):
title = "Login Screen"
def build(self):
return MyScreenManager()
if __name__ == '__main__':
TestApp().run()
#:kivy 1.10.0
<MyScreenManager>:
LoginScreen:
id: login
Screen2:
<LoginScreen>:
name: "screen1"
GridLayout:
size_hint: (0.5, 0.5)
pos_hint: {"center_x": 0.5, "center_y": 0.6}
rows: 3
padding: 20
Label:
size_hint: (0.2, 0.2)
text:"Password:"
font_size: 30
halign: "center"
valign: "middle"
TextInput:
id: password
size_hint: (0.2, 0.06)
cursor_blink: True
font_size: 20
multiline: False
password: True
Button:
text: "Continue"
size_hint: (0.2, 0.08)
on_release:
root.manager.ids.login.check_password(root.manager, password.text)
<Screen2>:
name: "screen2"
BoxLayout:
Label:
text: "Hello"
Button:
text: "Go to screen 1"
size: (100,300)
size_hint: (None,None)
on_press: root.manager.current = "screen1"