如何在矩形内添加文字?我正在使用下面的代码,并在画布内添加了一个标签,希望它将显示在矩形内。
import kivy
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import *
class MyApp(App):
def build(self):
r = AnchorLayout()
f = RelativeLayout()
g = GridLayout(cols=3, rows=5)
v = Video(source='driver.mp4', state='play', options={'eos':'loop'})
l1 = Label(text="jenkins", font_size=32)
l2 = Label(text="git", font_size=32)
f.add_widget(v)
f.add_widget(g)
g.add_widget(l1)
g.add_widget(l2)
with g.canvas:
Color(.4, .1, .1)
Rectangle(pos=(100,10), size=(100,100))
Label(text="KSHK")
return f
if __name__ == "__main__":
MyApp().run()
更新 这不会将文本对齐ColoredLabel的中心
import kivy
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import *
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from random import random
from kivy.properties import ListProperty
kv = '''
<ColoredLabel>:
background_color:
canvas.before:
Color:
rgba: self.background_color
Rectangle:
pos: (10,10)
size: (100,100)
'''
Builder.load_string(kv)
class ColoredLabel(Label):
background_color = ListProperty((0,0,0,1))
class MyApp(App):
def build(self):
f = AnchorLayout()
g = GridLayout(cols=2, rows=2)
layout = BoxLayout(size_hint=(1, None), height=50)
v = Video(source='driver.mp4', state='play', options={'eos':'loop'})
l1 = Label(text="jenkins", font_size=32)
l2 = Label(text="git", font_size=32)
f.add_widget(v)
label = ColoredLabel(text="Hello World!", background_color=(random(), random(), random(), 1))
g.add_widget(label)
f.add_widget(g)
return f
if __name__ == "__main__":
MyApp().run()
答案 0 :(得分:5)
将Label
与自定义背景颜色一起使用,然后将其放入布局中。例如:
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty
from kivy.app import App
from kivy.lang import Builder
from random import random
kv = '''
<ColoredLabel>:
background_color:
canvas.before:
Color:
rgba: self.background_color
Rectangle:
pos: self.pos
size: self.size
'''
Builder.load_string(kv)
class ColoredLabel(Label):
background_color = ListProperty((0, 0, 0, 1))
class TestApp(App):
def build(self):
layout = BoxLayout(size_hint=(1, None), height=50)
for label in ('a', 'b', 'c', 'd'):
label = ColoredLabel(text=label, background_color=(random(), random(), random(), 1))
layout.add_widget(label)
return layout
if __name__ == '__main__':
TestApp().run()
<强>更新强>
修复OP的问题:
import kivy
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import *
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from random import random
from kivy.properties import ListProperty
kv = '''
<ColoredLabel>:
size: (100,100)
pos: (10,10) # no effect
background_color:
canvas.before:
Color:
rgba: self.background_color
Rectangle:
pos: self.pos
size: self.size
'''
Builder.load_string(kv)
class ColoredLabel(Label):
background_color = ListProperty((0,0,0,1))
class MyApp(App):
def build(self):
f = AnchorLayout()
g = GridLayout(cols=2, rows=2)
layout = BoxLayout(size_hint=(1, None), height=50)
# v = Video(source='driver.mp4', state='play', options={'eos':'loop'})
l1 = Label(text="jenkins", font_size=32)
l2 = Label(text="git", font_size=32)
# f.add_widget(v)
label = ColoredLabel(text="Hello World!", size_hint=(None, None), background_color=(random(), random(), random(), 1))
g.add_widget(label)
f.add_widget(g)
return f
if __name__ == "__main__":
MyApp().run()
请注意,Rectange
的{{1}}需要ColoredLabel
和pos: self.pos
来标记实际所在的位置。这就是为什么我在上面的级别中更改了Label本身的大小。画布大小绑定到它。至于位置,它由放置size: self.size
的布局控制,因此从ColoredLabel
类改变它并没有太大变化。将其与ColoredLabel
:
FloatLayout
在这种情况下,更改import kivy
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import *
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from random import random
from kivy.properties import ListProperty
from kivy.uix.floatlayout import FloatLayout
kv = '''
<ColoredLabel>:
size: (150, 200)
pos: (50, 150)
background_color:
canvas.before:
Color:
rgba: self.background_color
Rectangle:
pos: self.pos
size: self.size
'''
Builder.load_string(kv)
class ColoredLabel(Label):
background_color = ListProperty((0,0,0,1))
class MyApp(App):
def build(self):
f = FloatLayout()
label = ColoredLabel(text="Hello World!", size_hint=(None, None), background_color=(random(), random(), random(), 1))
f.add_widget(label)
return f
if __name__ == "__main__":
MyApp().run()
内的pos实际上会产生影响。