作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试制作一个简单的ToDoList程序。有添加、删除和执行按钮。但我对标签颜色有一些错误。当我单击“执行”按钮时, ScrollView 中的标签颜色会发生变化,但是当我在其中一些完成后单击“删除”按钮时,彩色标签会发生变化。我是用 Canvas 做的。我该如何解决这个问题?
class Home(Screen):
def __init__(self,**kwargs):
super(Home,self).__init__(**kwargs)
def addWidget(self):
task_input = self.ids.task_input.text
newListItem = EachTask(text=task_input ,
id=str((len(self.ids.add_field.children))) )
print(newListItem.id)
self.ids.add_field.add_widget(newListItem)
class EachTask(BoxLayout):
def __init__(self, text= "", **kwargs):
super(EachTask,self).__init__(**kwargs)
self.ids.label.text = text
def Do_Task(self,instance):
child = instance.parent.parent
with self.canvas.before:
Color(.5,1,.2,1, mode='rgba')
Rectangle(pos=child.ids.label.pos, size=child.ids.label.size)
<FlatButton@ButtonBehavior+Label>:
font_size: 15
<Home>:
BoxLayout:
id: home
orientation: "vertical"
spacing: 5
#space_x: self.size[0]/2
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
size: self.size
pos: self.pos
##########HEADER#######
BoxLayout:
id: header
size_hint_y: None
height: 50
canvas.before:
Color:
rgba: (.85,.7,.2,1)
Rectangle:
size: self.size
pos: self.pos
Label:
text: "TO DO LIST"
font_size: "20sp"
bold: True
size_hint_x: .9
FlatButton:
text: "Back"
size_hint_x: .1
####################################
ScrollView:
canvas.before:
Color:
rgba: (1,1,.2,.2)
Rectangle:
size: self.size
pos: self.pos
BoxLayout:
id: add_field
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
spacing: 2 #Spaces between childs
#####################################################
BoxLayout:
id: input_field
size_hint_y: None
height: 80
TextInput:
id: task_input
focus: True
size_hint_x: .9
multiline: False
Button:
font_size: "40sp"
size_hint_x: .1
text: "+"
on_release: root.addWidget()
id: button1
color: 1,0.5,0.5,1
#######################################################
<EachTask>:
size_hint_y: None
height: 50
id: each_task
BoxLayout:
Label:
size_hint_x: .8
id: label
canvas.before:
Color:
rgba: (1,.2,.2,.2)
Rectangle:
size: self.size
pos: self.pos
Button:
size_hint_x: .1
text: "X"
on_release: app.root.ids.add_field.remove_widget(root)
Button:
size_hint_x: .1
text: "DO IT"
on_release: root.Do_Task(self)
最佳答案
需要对 kv 和 py 文件进行以下增强才能解决该问题。
rgba
Kivy automatically created & added an ObjectProperty
If the widget doesn’t have a property with the given name, an ObjectProperty will be automatically created and added to the widget.
rgba
并将其初始化为默认颜色 (1, .2, .2, .2)
类(class)规则,<EachTask>:
root.rgba
<EachTask>:
rgba: (1,.2,.2,.2) # Kivy auto created & added ObjectProperty, "rgba"
...
BoxLayout:
Label:
size_hint_x: .8
id: label
canvas.before:
Color:
rgba: root.rgba
...
Do_Task()
中的所有代码self.rgba = [.5, 1, .2, 1]
由此self
指当前小部件,即 EachTask
对象。def Do_Task(self, instance):
self.rgba = [.5, 1, .2, 1]
rgba
rgba: (1,.2,.2,.2)
与 root.rgba
<EachTask>:
...
BoxLayout:
Label:
size_hint_x: .8
id: label
canvas.before:
Color:
rgba: root.rgba
...
from kivy.properties import ListProperty
rgba
的ListProperty
输入并将其初始化为默认颜色,[1, .2, .2, .2]
上课EachTask()
Do_Task()
中的所有代码self.rgba = [.5, 1, .2, 1]
由此self
指当前小部件,即 EachTask
对象。from kivy.properties import ListProperty
...
class EachTask(BoxLayout):
rgba = ListProperty([1, .2, .2, .2])
...
def Do_Task(self, instance):
self.rgba = [.5, 1, .2, 1]
关于python - 如何在 Kivy 中动态更改标签背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57080830/
我是一名优秀的程序员,十分优秀!