2017-09-03 144 views
0

我正在做一个在线kivy教程。以下是我写的代码。这与教程视频中的一样。.kv只允许一个根对象

但是当我运行的代码,我得到了以下错误:

... 
    1:import FadeTransition kivy.uix.screenmanager.FadeTransition 
    2: 
>> 3:ScreenManagement: 
    4: transition: FadeTransition() 
    5: 
... 
    Only one root object is allowed by .kv 

我认为错误是与FadeTransition因为我有两个实例,MainScreen和AnotherScreen,但我不知道如何纠正它。在教程视频中,代码正常运行。那么为什么它不在我的电脑中执行?

下面是我事先写好

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition 


class MainScreen(Screen): 
    pass 

class AnotherScreen(Screen): 
    pass 

class ScreenManagement(ScreenManager): 
    pass 



presentation = Builder.load_string(''' 
#:import FadeTransition kivy.uix.screenmanager.FadeTransition 

ScreenManagement: 
    transition: FadeTransition() 
    MainScreen: 
    AnotherScreen: 


<MainScreen>: 
    name: "main" 
    Button: 
     text: "Next Screen" 
     font_size: 50 
     on_release: root.app.current = "other" 

<AnotherScreen>: 
    name: "other" 
    Button: 
     text: "Back Home" 
     font_size: 50 
     on_release: root.app.current = "main" 

''') 


class MainApp(App): 
    def build(self): 
     return presentation() 

if __name__=="__main__": 
    MainApp().run() 

感谢代码。

+0

,我建议一些解决您的代码布局。但我不知道答案 –

回答

0

我在观看同一个视频时遇到了确切的问题。你需要做到这一点,而不是(假设你要淡出过渡):

Main.py文件:

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition 

class ScreenManagement(ScreenManager): #here you are creating a screen manager called ScreenManagement 
    pass 
class MainScreen(Screen): 
    pass 

class AnotherScreen(Screen): 
    pass 

presentation = Builder.load_file("Main2.kv") #telling the app which .kv file to use 

class MainApp(App): 
    def build(self): 
     return presentation 

runMain = MainApp() 
runMain.run() 

现在到你Main2.kv文件:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition 

ScreenManagement: 
    transition: FadeTransition() #telling the screen manager to use a fade transition 
    MainScreen: 
    AnotherScreen: 

<MainScreen>: 
    name: "main" #this name is what the screen manager uses to distinguish which screen to go to 
    Button: 
     text: "Go To Next Screen" 
     color:0,0,0,1 
     background_color: 1,1,1,1 
     size_hint: 1,0.10 
     pos:0,200 
     font_size: 30 
     on_release: app.root.current = "another" #the screen that the screen manager is told to go to 

<AnotherScreen>: 
    name: "another" 
    Button: 
     text: "Go To Main Screen" 
     color:0,0,0,1 
     background_color: 1,1,1,1 
     size_hint: 1,0.10 
     pos:0,200 
     font_size: 30 
     on_release: app.root.current = "main" 

希望有所帮助。

0

在您的kv文件中,您需要使用“<”和“>”围绕ScreenManagement类,如下例所示。

main.py

from kivy.app import App 
from kivy.uix.screenmanager import ScreenManager, Screen 


class ScreenManagement(ScreenManager): 
    pass 


class MainScreen(Screen): 
    pass 


class AnotherScreen(Screen): 
    pass 


class MainApp(App): 
    def build(self): 
     return ScreenManagement() 

if __name__ == "__main__": 
    MainApp().run() 

main.kv

#: import FadeTransition kivy.uix.screenmanager.FadeTransition 

<ScreenManagement>: 
    transition: FadeTransition() 
    MainScreen: 
    AnotherScreen: 

<MainScreen>: 
    name: "main" 
    Button: 
     text: "Go To Next Screen" 
     color:0,0,0,1 
     background_color: 1,1,1,1 
     size_hint: 1,0.10 
     pos:0,200 
     font_size: 30 
     on_release: app.root.current = "another" 

<AnotherScreen>: 
    name: "another" 
    Button: 
     text: "Go To Main Screen" 
     color:0,0,0,1 
     background_color: 1,1,1,1 
     size_hint: 1,0.10 
     pos:0,200 
     font_size: 30 
     on_release: app.root.current = "main" 

输出

enter image description here