我目前正在尝试实现一个自定义小部件,其文本可以有两种颜色。它几乎可以工作,但我有两个问题,第一个问题是:为什么绿色和红色 BoxLayout div(附图中的黑条)之间有间隙?第二个问题是如何使分割标签的宽度为一个 div 而不是两个? 我尝试了很多方法,但到目前为止没有任何效果。非常感谢任何建议/想法!预先感谢
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string('''
<Split>:
orientation:"vertical"
NormalLabel
NormalLabel
BoxLayout:
orientation:"vertical"
size_hint:1,None
TopHalfLabel
BottomHalfLabel
NormalLabel
NormalLabel
<TopHalfLabel>:
size_hint:1,None
markup:True
text:"[color=#FF0000]How can I do this?[/color]"
font_size:30
height: self.texture_size[1]
canvas.before:
Color:
rgba: 1, 0, 0, 0.3 # Border color (red in this example)
Rectangle:
pos: self.pos
size: self.size
<BottomHalfLabel>:
size_hint:1,None
markup:True
text:"[color=#FFFF00]How can I do this?[/color]"
font_size:30
height: self.texture_size[1]
canvas.before:
Color:
rgba: 0, 0, 1, 0.3 # Border color (red in this example)
Rectangle:
pos: self.pos
size: self.size
<NormalLabel>:
size_hint:1,None
markup:True
text:"[color=#FFFF00]How can I do this?[/color]"
font_size:30
height: self.texture_size[1]#
canvas.before:
Color:
rgba: 0, 1, 0, 0.3 # Border color (red in this example)
Rectangle:
pos: self.pos
size: self.size
''')
class NormalLabel(Label):
pass
class TopHalfLabel(Label):
def on_texture_size(self, instance, value):
#self.texture = self.texture.get_region(0, self.texture.height/2, self.texture.width, self.texture.height/2)
self.texture = self.texture.get_region(0, self.texture.height/2, self.texture.width, self.texture.height)
class BottomHalfLabel(Label):
def on_texture_size(self, instance, value):
#self.texture = self.texture.get_region(0, 0, self.texture.width, self.texture.height/2)
self.texture = self.texture.get_region(0, -self.texture.height/2, self.texture.width, self.texture.height)
class Split(BoxLayout):
pass
class MyApp(App):
def build(self):
return Split()
if __name__ == '__main__':
MyApp().run()
您可以通过在
Widget
规则前添加 kv
来重新定义 -
的样式。请参阅文档。所以,我修改了你的kv
规则,如下:
<-TopHalfLabel>:
size_hint:1,None
markup:True
text:"[color=#FF0000]How can I do this?[/color]"
font_size:30
height: self.texture_size[1]/2
canvas.before:
Color:
rgba: 1, 0, 0, 0.3 # Border color (red in this example)
Rectangle:
pos: self.pos
size: self.size
canvas:
Color:
rgba: 1, 1, 1, 1
Rectangle:
texture: self.new_texture
size: self.texture_size[0], self.texture_size[1]/2
pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1]/4)
<-BottomHalfLabel>:
size_hint:1,None
markup:True
text:"[color=#FFFF00]How can I do this?[/color]"
font_size:30
height: self.texture_size[1]/2
canvas.before:
Color:
rgba: 0, 0, 1, 0.3 # Border color (red in this example)
Rectangle:
pos: self.pos
size: self.size
canvas:
Color:
rgba: 1, 1, 1, 1
Rectangle:
texture: self.new_texture
size: self.texture_size[0], self.texture_size[1]/2
pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1]/4)
主要更改是将
height
设置为纹理高度的一半,并添加 canvas
部分,该部分基于 kv
的默认 Label
,但使用已定义的 new_texture
属性通过修改类:
class TopHalfLabel(Label):
new_texture = ObjectProperty(None)
def on_texture_size(self, instance, value):
# self.texture = self.texture.get_region(0, self.texture.height/2, self.texture.width, self.texture.height/2)
self.new_texture = self.texture.get_region(0, self.texture.height / 2, self.texture.width, self.texture.height/2)
class BottomHalfLabel(Label):
new_texture = ObjectProperty(None)
def on_texture_size(self, instance, value):
# self.texture = self.texture.get_region(0, 0, self.texture.width, self.texture.height/2)
self.new_texture = self.texture.get_region(0, 0, self.texture.width, self.texture.height/2)
我还在包含的
height: self.minimum_height
中添加了一个BoxLayout
:
<Split>:
orientation:"vertical"
NormalLabel
NormalLabel
BoxLayout:
orientation:"vertical"
size_hint:1,None
height: self.minimum_height
TopHalfLabel
BottomHalfLabel
NormalLabel
NormalLabel
我认为这符合你的要求。