如何在.kv文件中的Button的on_press事件中隐藏画布和矩形

时间:2018-01-26 13:13:54

标签: python kivy

我已经在kivy创建了一个登录界面,我想在按下按钮后隐藏或禁用画布和矩形,请帮助我。这是我的代码

  

:                 名字:" cany"                 canvas.before:                       颜色:                             rgba:1,1,1,1                       长方形:                             尺寸:self.size                             pos:self.pos                             来源:' rect4190.png'             标签:                   ID:user_lbl                   文字:"用户名:"                   颜色:.9,.1,.1,1                   pos:40,400

      TextInput:
            id:username_txt
            size: cm(4),cm(0.66)
            pos: 125,437
            write_tab: False
            multiline: False

      Label:
            id:pass_lbl
            text: 'Password:'
            color: .9, 0.12, .1, 1
            pos: 40,350

      TextInput:
            id:password_txt
            size: cm(4),cm(0.66)
            pos: 125,387
            write_tab: False
            multiline: False

      Button:
            id:btn_login
            size: cm(2.33),cm(0.66)
            text: 'Login'
            pos:  160,200
            focus: True
            on_press:
                  root.validate(username_txt.text,password_txt.text)
                  user_lbl.opacity = 0
                  pass_lbl.opacity = 0
                  username_txt.opacity = 0
                  password_txt.opacity = 0
                  btn_login.opacity = 0

1 个答案:

答案 0 :(得分:0)

在你的python文件中:

from kivy.properties import NumericProperty

在python文件的Login类中:

canvas_opacity = NumericProperty(1, rebind=True)

def vanish_canvas(self):
    self.canvas_opacity = 0

在你的kv文件中,

<LoginScreen>:
    name: "cany" 
    canvas.before: 
        Color: 
            rgba: 1,1,1, root.canvas_opacity
        Rectangle: 
            size: self.size 
            pos: self.pos 
            source: 'rect4190.png' 

    Button:
        text: 'clear canvas'
        on_press: root.clear_canvas()

这样做不会删除图像,但它会使所讨论的画布中的所有内容都不可见。

Canvas确实有一个内置的清晰功能,但似乎它会清除所有内容,包括你不想摆脱的小部件。