.kv只允许一个根对象

时间:2017-09-03 11:40:42

标签: python kivy

我正在做一个在线kivy教程。下面是我写的代码。它与教程视频完全一样。

但是当我运行代码时,我收到以下错误:

 ...
     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()

提前致谢。

3 个答案:

答案 0 :(得分:2)

试试这段代码,它可能会有所帮助,我花了很长时间才弄明白:

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 = ('''
#: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 Builder.load_string(presentation)

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

答案 1 :(得分:0)

在观看同一视频时,我遇到了确切的问题。您将需要这样做(假设您想要淡入淡出过渡):

Main.py文件:

private static void findRecurrence(String input) {
    final Map<String, Integer> map = new LinkedHashMap<>();
    for(int i=0; i<input.length(); ) {
        int pointer = i;
        int startPointer = i;
        boolean pointerHasIncreased = false;
        for(int j=0; j<startPointer; j++){
            if(pointer<input.length() && input.charAt(j)==input.charAt(pointer) && input.charAt(j)!=32){
                pointer++;
                pointerHasIncreased = true;
            }else{
                if(pointerHasIncreased){
                    break;
                }
            }
        }
        if(pointer - startPointer >= 2) {
            String word = input.substring(startPointer, pointer);
            if(map.containsKey(word)){
                map.put(word, map.get(word)+1);
            }else{
                map.put(word, 1);
            }
            i=pointer;
        }else{
            i++;
        }
    }
    for(Map.Entry<String, Integer> entry : map.entrySet()){
        System.out.println(entry.getKey() + " = " + (entry.getValue()+1));
    }
}

现在转到您的Main2.kv文件:

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()

希望有所帮助。

答案 2 :(得分:0)

在你的kv文件中,你需要用&#34;&lt;&#34;来围绕ScreenManagement类。和&#34;&gt;&#34;如下例所示。

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