Kivy-如何设置一段代码的持续时间?

时间:2018-09-22 15:29:19

标签: python button kivy duration kivy-language

我有:

Button:
    text: "SEQUENCE 2"
    size_hint: None, .16
    width: 225
    on_press:
        self.background_color = (1.7, 0, 1.7, 1)

我希望'self.background_color'在'on_press'之后的几秒钟后恢复正常(按之前的状态)。

我该怎么做?

编辑(在.kv StackLayout中是所需位置。)

<ContScreen>:
    StackLayout
        orientation: "tb-rl"
        spacing: 15

        Button:
            text: "SEQUENCE 1"
            size_hint: None, .16
            width: 225

        Button:
            text: "SEQUENCE 2"
            size_hint: None, .16
            width: 225

如果无法集成到.kv StackLayout中,我如何基于'<>'方法重新格式化它以使其工作。

1 个答案:

答案 0 :(得分:0)

  1. 使用Clock.create_trigger()创建触发事件。
  2. 持续时间到期或已满时,将按钮的background_color重设为默认颜色[1、1、1、1]

示例

main.py

#:kivy 1.11.0

<ButtonBackgroundColourDemo>:
    text: "SEQUENCE 2"
    size_hint: None, .16
    width: 225

test.kv

let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {                                                 
                return
            } 

输出

Img01 Img02

相关问题