2014-07-14 98 views
3

如何在矩形內添加文本?我正在使用下面的代碼,並在畫布中添加了一個標籤,希望它會顯示在矩形內。python kivy在矩形內添加文本

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() 

回答

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() 

注意ColoredLabelRectange需要有pos: self.possize: 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() 

在這種情況下,改變ColoredLabel POS裏面居然有效果。

+0

我試過這個方法,但是當我在AnchorLayout/Relative佈局的頂部添加布局部件時,文本沒有在框中居中...... – krisdigitx

+0

感謝Nykakin,這個工作.. – krisdigitx