【发布时间】:2020-03-10 17:30:24
【问题描述】:
我试图让这个简单的脚本在使用 on_press 行为按下时更改按钮的颜色。
Python 脚本:kivytest.py
#Import Libraries
#Import Kivy GUI
import kivy
#From kivy, import base class App
# app: always refers to instance of this application
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.stacklayout import StackLayout
################################################################################
# Creating the root widget used in the .kv file
class testLayout(StackLayout):
pass
################################################################################
class kivytestApp(App):
def build(self):
return testLayout()
test = kivytestApp()
test.run()
对应的.kv文件:kivytest.kv
<StackLayout>:
#Create the canvas
canvas:
#Color the canvas background a dark gray with alpha = 1
Color:
rgba: 0.11, 0.15, 0.17, 1
Rectangle:
pos: self.pos
size: self.size
#Provide the orientation of the stack layout from left to right / top to bottom
orientation: "lr-tb"
#Add padding between children and layout
padding: 10
#Add spacing between each of its children
spacing: 5
Button:
size_hint: 0.2, None
text: "second button"
background_color: [1, 0, 0, 1]
background_normal: ''
on_press:
background_color: [0, 1, 0, 1]
print("second button pressed")
不幸的是,当我按下应用程序中的按钮时,颜色仍然是红色,但 on_press 上的红色稍微深一些,而不是我指定的绿色。我应该纠正什么?这个实现不是看起来那么简单吗?
【问题讨论】: