Kivy颜色更改与按钮 [英] Kivy Color Change with Button

查看:2498
本文介绍了Kivy颜色更改与按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Kivy和Python编写一小段代码。我试图让它,所以当你点击一个按钮,按钮的文字改变颜色。

I am currently working on a bit of code using Kivy and Python. I am trying to make it so when you click a button, the button's text changes color.

当我点击按钮,但是,颜色不像我想要它。

When I click on the button, however, the color doesn't like I want it to.

任何想法如何可以解决这个问题?我只是在学习Kivy,也许答案比我想象的容易。 .py文件在以下

Any ideas how this can be fixed? I am just learning Kivy and perhaps the answer is easier than I think. The .py file is below

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.graphics import Color
class TextBubbleSample(Widget):
    bubble = ObjectProperty(None)

class TextBubble(Widget):
        pass

class Talk(Button):
    btn = Button
    def button_press(self):
        btn.bind(on_state = self.on_event)

def on_event(self):
        btn.color = 1,0,0,1

class TextBubbleApp(App):
    def build(self):
        return TextBubbleSample()

if __name__ == '__main__':
    TextBubbleApp().run()




and here is the .kv file

#:kivy 1.0.9

<TextBubbleSample>:
    bubble: text_bubble
    btn: click_here

    TextBubble:
        id: text_bubble
        x: root.x
        center_y: root.center_y

    Talk:
        id: click_here
        x: 10
        center_y: 220
        text: "Talk to me."
        color: 0,0,1,1

<TextBubble>:
    canvas:
        Color:
            rgba: 1,0,0,1
        Rectangle:
            pos: 10, 10
            size: 780, 150

    Label:
        color: 0,0,1,1
        font_size: 35  
        center_x: 200
        top: root.top - 200
        text: "I am talking"


推荐答案

您不应该/不能使用变量 btn 。使用 self.bind(on_state = self.on_event)然后 self.color =(1,0,0,1)

You shouldn't/can't use the variable btn like that. Use self.bind(on_state=self.on_event) then self.color = (1, 0, 0, 1).

这篇关于Kivy颜色更改与按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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