2014-07-15 95 views
0

我用這個方法蟒蛇kivy BoxLayout的設置位置

layout = BoxLayout(size_hint=(1, None), height=50, spacing=100, pos_hint={'y': 0.5 , 'top': 0.5}) 

但它在中間中心定位BoxLayout的,我希望它被放置在FloatLayout的頂級中鋒。

而且在運行時給出了這樣的警告代碼....

[CRITICAL   ] [Clock  ] Warning, too much iteration done before the next frame. Check your code, or increase the Clock.max_iteration attribute 
[CRITICAL   ] [Clock  ] Warning, too much iteration done before the next frame. Check your code, or increase the Clock.max_iteration attribute 
[CRITICAL   ] [Clock  ] Warning, too much iteration done before the next frame. Check your code, or increase the Clock.max_iteration attribute 
[CRITICAL   ] [Clock  ] Warning, too much iteration done before the next frame. Check your code, or increase the Clock.max_iteration attribute 
[CRITICAL   ] [Clock  ] Warning, too much iteration done before the next frame. Check your code, or increase the Clock.max_iteration attribute 
[INFO    ] [Base  ] Leaving application in progress... 
下面

是代碼:

import kivy 
from kivy.app import App 
from kivy.uix.anchorlayout import AnchorLayout 
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.widget import Widget 
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) 

    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() 
     g = GridLayout(cols=5, rows=2, row_force_default=True, row_default_height=80) 
     layout = BoxLayout(size_hint=(1, None), height=50, spacing=100, pos_hint={'y': 0.5 , 'top': 0.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) 

     """ 
     label1 = ColoredLabel(text="jenkins output", pos_hint={'top': 1, 'right': .1}, size_hint=(None, None) , background_color=(160,160,160,.5)) 
     f.add_widget(label1) 

     label2 = ColoredLabel(text="git output", pos_hint={'top': 1, 'right': .5}, size_hint=(None, None) , background_color=(160,160,160,.5)) 
     f.add_widget(label2) 

     label3 = ColoredLabel(text="dev output", pos_hint={'top': 1, 'right': .8}, size_hint=(None, None) , background_color=(160,160,160,.5)) 
     f.add_widget(label3) 
     """ 

     text1 = "jenkins" 

     label1 = ColoredLabel(text=text1, background_color=(160,160,160,.5)) 
     layout.add_widget(label1) 

     label2 = ColoredLabel(text="git", background_color=(160,160,160,.5)) 
     layout.add_widget(label2) 

     label3 = ColoredLabel(text="portal", background_color=(160,160,160,.5)) 
     layout.add_widget(label3) 

     f.add_widget(layout) 


     return f 

if __name__ == "__main__": 
    MyApp().run() 

回答

2

使用pos_hint={'top': 1}。不要混合使用topy,這兩者都與自己相沖突,因爲它們都指的是verical position,這是您erros的原因。

from kivy.app import App 
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.label import Label 
from kivy.lang import Builder 
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import ListProperty 

kv = ''' 
<ColoredLabel>: 
    size: (100,100) 

    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() 
     layout = BoxLayout(size_hint=(1, None), height=50, pos_hint={'top': 1}) 

     label1 = ColoredLabel(text="jenkins", background_color=(160,160,160,.5)) 
     layout.add_widget(label1) 

     label2 = ColoredLabel(text="git", background_color=(160,160,160,.5)) 
     layout.add_widget(label2) 

     label3 = ColoredLabel(text="portal", background_color=(160,160,160,.5)) 
     layout.add_widget(label3) 

     f.add_widget(layout) 


     return f 

if __name__ == "__main__": 
    MyApp().run() 

還檢查了使用效果的:

layout = BoxLayout(size_hint=(0.5, None), height=50, pos_hint={'top': 1}) 

和:

layout = BoxLayout(size_hint=(0.5, None), height=50, pos_hint={'top': 1, 'center_x':0.5}) 
+0

感謝Nykakin細的工作.... – krisdigitx