【发布时间】:2021-07-13 16:11:41
【问题描述】:
我想更改点击时的背景颜色。这似乎并不难,但问题是我在另一个 Kivy 文件中有按钮,而不是在 main.js 文件中。如何从button 获取属性并更改其颜色?
这是我的尝试(代码):
Python:
easy_button = ObjectProperty()
def easy_button(self):
self.GAME_SPEED = 0.5
MainKV:
#:import menu menu
Game:
<Game>:
menu_canvas: menu_canvas
score_widget: score_widget
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'images/pixel.jpg'
Label:
font_name: "fonts/PermanentMarker-Regular.ttf"
text: "SCORE " + str(root.score)
size_hint: .2, .2
pos_hint: {"x": .03, "y": .8}
font_size: self.width/3.5
opacity: 0
id: score_widget
MenuWidget:
id: menu_canvas
Second Kivy File:
<MenuWidget>:
Button:
text: "Easy"
font_name: "fonts/PermanentMarker-Regular.ttf"
size_hint: .18, .15
background_normal: ''
background_color: .5, 1, .5, .85
on_press: root.parent.easy_button()
pos_hint: {"x": .2, "y": .1}
font_size: self.width/5.5
id: easy_button
【问题讨论】:
-
这能回答你的问题吗? How to change background colour in Kivy