为什么kivy一次更改所有小部件 [英] Why kivy changes all widgets at once

查看:96
本文介绍了为什么kivy一次更改所有小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建Button小部件的子类.在处理on_touch_down事件时,我想更改按钮文本以进行调试.当我单击一个按钮时,此类型的所有按钮的文本都更改了.为什么?如何仅更改单击按钮上的文本?

I create a subclass of the Button widget. While handling on_touch_down event I want to change button text for debugging purpose. When I click a single button, all the buttons of this type got their text changed. Why? How to change text on a clicked button only?

按钮类:

class FigureButton(Button):
    def on_touch_down(self, touch):
        super(FigureButton, self).on_touch_down(touch)
        self.text = 'Clicked'

.kv文件部分:

FigureButton:
    text: '1'
FigureButton:
    text: '2'

谢谢!

推荐答案

on_touch_down不进行碰撞检查,因此小部件可以与其区域外的触摸进行交互.您可以在代码之前添加'if self.collide_point(*touch.pos): ...(并将添加的内容缩进if语句中)以按预期进行工作.

on_touch_down doesn't do collision checking, so that widgets can interact with touches outside their area. You can add 'if self.collide_point(*touch.pos): ... before your code (and indent your additions into the if statement) to have this work as you expect.

正如您所发现的,on_press事件/方法确实进行了冲突检查.无论如何,这通常是与按钮进行交互的更好方法.

As you have discovered, the on_press event/method does do collision checking. This is normally a better way to interact with buttons anyway.

这篇关于为什么kivy一次更改所有小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆