无法在kv lang中使用过渡

时间:2016-10-29 16:01:25

标签: python python-2.7 kivy kivy-language

应用代码:

public class Test {

private static Product[] products;

public static void main(String[] args) {
    doSomething((p) -> p.setName("CLOTHING")); //first loop
    doSomething((p) -> p.setId(1)); //second loop
}

public static void doSomething(Consumer<Product> productConsumer){
    products = getProducts();
    for(Product p : products){
        productConsumer.accept(p);
        System.out.println(p.toString());
    }

}

private static Product[] getProducts(){
    Product pr1 = new Product();
    pr1.setId(1);
    pr1.setName("Jacket");

    Product p2 = new Product();
    p2.setId(2);
    p2.setName("Shoe");

    return new Product[]{pr1, p2};
}

}

public class Product {

private int id;
private String name;
}

每次执行时我都有以下追溯。它有什么问题?

from kivy.app import App
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.screenmanager import Screen
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import FadeTransition<br/>
kv = '''
ScreenManagement:
    transition: FadeTransition()
    screen1:
    screen2:
<screen1>:
    name: 'one'
    Button:
        text: 'Hello!'
        on_release: app.root.current = 'two'
<screen2>:
    name: 'two'
    Button:
        text: 'Hello2'
        on_release: app.root.current = 'one'
'''<br/>
class screen1(Screen):
    pass
class screen2(Screen):
    pass
class ScreenManagement(ScreenManager):
    pass
class test23(App):
    def build(self):
        return Builder.load_string(kv)
        <br/>
if __name__ == '__main__':
    test23().run()

2 个答案:

答案 0 :(得分:3)

转换不是小部件,也不是属性,因此默认情况下,在创建用于解析.kv文件或kv字符串的环境时,它们不会被导入,因此您可以使用它们。我需要将它导入.kv文件/字符串的顶部。

示例:

#:import FadeTransition kivy.uix.screenmanager.FadeTransition

答案 1 :(得分:0)

在kv文件的顶部,您需要导入。为了获得转换,它将是

#:import sm kivy.uix.screenmanager

屏幕管理:     过渡:sm.FadeTransition()     屏蔽1:     屏蔽2:     ....

我就是这样做的,这适用于所有转换,可以在https://kivy.org/docs/api-kivy.uix.screenmanager.html找到完整的文档

我正在使用python 2.7和kivy 1.10.0