【发布时间】:2016-12-26 23:10:37
【问题描述】:
我正在尝试使 BoxLayout 内的 ScreenManager 工作,因此我可以在屏幕的每个屏幕下方都有一个固定工具箱。我设法显示了第一个屏幕(感谢this question),但是,当我尝试切换到另一个屏幕时,应用程序崩溃了,说没有其他屏幕。
实际上,真的没有其他屏幕:屏幕管理的 init 内的两个打印都没有显示任何内容。我不知道为什么。
没有工具栏(当然,只有 ScreeManager(ment) 和代码中的必要调整)一切正常。
我尝试将_widget 添加到 ScreenManagement 并填充了 screen_names,但我无法在屏幕之间切换。
我错过了什么?
错误的最后部分:
screen = self.get_screen(value)
File "C:\Python27\lib\site-packages\kivy\uix\screenmanager.py", line 944, in get_screen
raise ScreenManagerException('No Screen with name "%s".' % name)
ScreenManagerException: No Screen with name "init".
Windows 7、Python 2.7、Kivy 1.9.1
这是 ClassApp.py:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.clock import Clock
#just solving my weak GPU issue
from kivy import Config
Config.set('graphics', 'multisamples', '0')
kivy.require('1.9.1')
class Init(Screen):
pass
class Menu(Screen):
pass
class ScreenManagement(ScreenManager):
def __init__(self,**kwargs):
print('before super: ', self.screen_names)
super(ScreenManagement,self).__init__(**kwargs)
print('after super: ', self.screen_names)
def switch_to_menu(self):
self.current = 'menu'
print('going to menu')
def switch_to_init(self):
self.current = 'init'
print('going to init')
class ClassAllScreen(BoxLayout):
sm = ScreenManagement()
sm.transition = NoTransition()
pass
class ClassApp(App):
def build(self):
self.root = ClassAllScreen()
return self.root
if __name__ == '__main__':
ClassApp().run()
这里是 Class.kv:
<Init>: #first Screen
name: 'init'
BoxLayout:
orientation:'vertical'
padding:20
spacing:10
Button:
text:'uppest'
GridLayout:
spacing:10
cols:2
Button:
text:'upper left'
Button:
text:'upper right'
Button:
text:'bottom left'
Button:
text:'bottom right'
Button:
text:'bottomest: go to menu'
on_press:app.root.sm.switch_to_menu()
<Menu>: #second Screen
name: 'menu'
BoxLayout:
orientation:'vertical'
padding:20
spacing:10
GridLayout:
spacing:10
cols:2
Button:
text:'upper left'
Button:
text:'upper right'
Button:
text:'bottom left'
Button:
text:'bottom right'
Button:
text:'bottomy'
Button:
text:'bottomest: go to init'
on_press:app.root.sm.switch_to_init()
<ScreenManagement>: #including both the Screens in ScreenManager
Menu:
Init:
<ClassAllScreen>: #all the display with ScreenManager and "Toolbar"
orientation:'vertical'
ScreenManagement:
BoxLayout:
size_hint_y: None
height: 60
spacing: 5
padding: 5,5,0,5
canvas:
Color:
rgba: .1,.1,.1,1
Rectangle:
pos: self.pos
size: self.size
Button:
text:'1'
size_hint_x: None
width: 60
Button:
text:'2'
size_hint_x: None
width: 60
Button:
text:'3'
size_hint_x: None
width: 60
【问题讨论】:
标签: python python-2.7 kivy kivy-language