这段代码有什么问题? (它显示一个空屏幕) 我想有2个屏幕,每个屏幕上有1个按钮,只需添加一个小部件(来自python代码),然后按1个按钮切换到另一个屏幕。 当我运行代码时,我可以看到没有按钮且没有标签的空白屏幕。如何解决这一切?
PS:当我执行以下操作时,它完全有效:
class MyScreenManager(ScreenManager):
pass
# ... and
class MyAppli(App):
def build(self):
Window.clearcolor=(1,1,1,1)
return MyScreenManager()
debug.py:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.lang import Builder
class MyScreenManager(ScreenManager):
def initialize(self): # I know this is not necessary,
# but in my real code, i have to proceed like this.
# ... (some instructions)
pass
class Menu1(Screen):
def add_label(self):
self.add_widget(Label(text="Hello", color=(1,0,0,1)))
class Menu2(Screen):
def add_label(self):
self.add_widget(Label(text="Hello", color=(1,0,0,1)))
Builder.load_file("debug.kv")
class MyAppli(App):
def build(self):
Window.clearcolor = (1,1,1,1)
return MyScreenManager().initialize()
if __name__ == '__main__':
MyAppli().run()
debug.kv:
#:kivy 1.9.1
<MyScreenManager>:
Menu1:
id: menu1
Menu2:
id: menu2
<Menu1>:
name: "screen1"
BoxLayout:
Label:
text:"Screen 1"
color:(1,0,0,1)
Button:
text:"Add a label"
size:(200,50)
size_hint:(None,None)
background_color:(1,0,0,1)
on_press:
root.manager.ids.menu1.add_label()
Button:
text:"Go to screen 2"
size:(200,50)
size_hint:(None,None)
background_color:(1,0,0,1)
on_press:
root.manager.current = "screen2"
<Menu2>:
name: "screen2"
BoxLayout:
Label:
text:"Screen 2"
color:(1,0,0,1)
Button:
text:"Add a label"
size:(200,50)
size_hint:(None,None)
background_color:(1,0,0,1)
on_press:
root.manager.ids.menu2.add_label()
Button:
text:"Go to screen 1"
size:(200,50)
size_hint:(None,None)
background_color:(1,0,0,1)
on_press:
root.manager.current = "screen1"
答案 0 :(得分:0)
请在MyScreenManager类中执行以下操作。有关详细信息,请参阅示例。
class MyScreenManager(ScreenManager):
def __init__(self, **kwargs):
super(MyScreenManager, self).__init__(**kwargs)
self.initialize()
def initialize(self): # I know this is not necessary,
# but in my real code, i have to proceed like this.
# ... (some instructions)
print("MyScreenManager.initialize called")
...
class MyAppli(App):
def build(self):
Window.clearcolor = (1, 1, 1, 1)
return MyScreenManager()
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.lang import Builder
class MyScreenManager(ScreenManager):
def __init__(self, **kwargs):
super(MyScreenManager, self).__init__(**kwargs)
self.initialize()
def initialize(self): # I know this is not necessary,
# but in my real code, i have to proceed like this.
# ... (some instructions)
print("MyScreenManager.initialize called")
class Menu1(Screen):
def add_label(self):
self.add_widget(Label(text="Hello", color=(1,0,0,1)))
class Menu2(Screen):
def add_label(self):
self.add_widget(Label(text="Hello", color=(1,0,0,1)))
Builder.load_file("debug.kv")
class MyAppli(App):
def build(self):
Window.clearcolor = (1, 1, 1, 1)
return MyScreenManager()
if __name__ == '__main__':
MyAppli().run()
#:kivy 1.10.0
<MyScreenManager>:
Menu1:
id: menu1
Menu2:
id: menu2
<Menu1>:
name: "screen1"
BoxLayout:
Label:
text:"Screen 1"
color:(1,0,0,1)
Button:
text:"Add a label"
size:(200,50)
size_hint:(None,None)
background_color:(1,0,0,1)
on_press:
root.manager.ids.menu1.add_label()
Button:
text:"Go to screen 2"
size:(200,50)
size_hint:(None,None)
background_color:(1,0,0,1)
on_press:
root.manager.current = "screen2"
<Menu2>:
name: "screen2"
BoxLayout:
Label:
text:"Screen 2"
color:(1,0,0,1)
Button:
text:"Add a label"
size:(200,50)
size_hint:(None,None)
background_color:(1,0,0,1)
on_press:
root.manager.ids.menu2.add_label()
Button:
text:"Go to screen 1"
size:(200,50)
size_hint:(None,None)
background_color:(1,0,0,1)
on_press:
root.manager.current = "screen1"