2017-07-17 47 views
0

我已经在这堵墙上跑了近一天了。我觉得我已经尝试了一切。首先,这是我想要的以及我的视觉。Kivy标签左对齐和调整大小

这是我现在所拥有的:

enter image description here

我想是这样的:

enter image description here

在图像 'CurrentHistory:\ nHeader:' 是单个标签。我可以让它左对齐,但我不能得到它没有巨大的填充顶部和底部。我喜欢标签是正方形的,而不是文字周围的矩形。我现在已经在Kivy'ing约2天,所以请原谅我,如果我错过了超级基本的东西。

编辑

我有一个全功能的演示更新。奇怪的是,.kv生成的标签完全符合我的要求,但是通过python生成的标签没有。

的SegmentLayout等同于.kv文件:(定义的网格布局

总体布局是这样的:.kv

#:kivy 1.8.0 

<MainWidget> 
    BoxLayout 
     size: root.size 

     ScrollView 
      id: scrlv 
      size_hint: .75, 1 

      GridLayout 
       cols: 1 
       size_hint: 1, None 
       height: max(self.minimum_height, scrlv.height) 

       canvas: 
        Color: 
         rgba: 150/255, 150/255, 150/255, 1 
        Rectangle: 
         pos: self.pos 
         size: self.size 

       SegmentLayout 

       GridLayout 
        cols: 1 
        size_hint: 1,None 
        height: self.minimum_height 

        Label 
         text: '[b]CurrentHistory:[/b]\nHeader:' 
         size_hint: (None, None) 
         halign: 'left' 
         markup: True 
         size: self.texture_size 

        TextInput 
         size_hint: (1,None) 
         height: self.minimum_height 
         text: 'Enter Text Here' 

     BoxLayout 
      orientation: "vertical" 
      size_hint: .25, 1 

      canvas: 
       Color: 
        rgba: 240/255, 180/255, 80/255, 1 
       Rectangle: 
        pos: self.pos 
        size: self.size 

      Label 
       text: "Label 1" 
      Label 
       text: "Label 2" 

蟒蛇驱动器演示

import kivy 

kivy.require('1.8.0') # replace with your current kivy version ! 

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.factory import Factory 

from kivy.uix.label import Label 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.textinput import TextInput 

class SegmentLayout(GridLayout): 

    def __init__(self, **kwargs): 
     super(SegmentLayout, self).__init__(cols=1, size_hint=(1,None), **kwargs) 
     self.bind(minimum_height=self.setter('height')) 

     label_text = '[b]CurrentHistory:[/b]\nHeader:' 

     label = Label(text=label_text, halign='left', size_hint=(None,None), markup=True) 
     label.bind(size=label.setter('texture_size'))  
     self.add_widget(label) 

     text_input = TextInput(text='Enter Text Here', size_hint=(1,None)) 
     text_input.bind(minimum_height=text_input.setter('height')) 
     self.add_widget(text_input) 

class MainWidget(Widget): 
    pass 

class MyApp(App): 

    def build(self): 
     return MainWidget() 

Factory.register('SegmentLayout', cls=SegmentLayout) 

if __name__ == '__main__': 
    MyApp().run() 
+0

我加了一个全功能的演示中,奇怪的是,当在简单的.kv文件中创建我想要的内容时,它会按预期工作,当通过python执行时会失败。 .kv文件做我希望发生的事情。 – Hangman4358

回答

0

我发现以下链接https://kivy.org/docs/api-kivy.uix.label.html#kivy.uix.label.Label.halign其中警告不要使用halign Property。我的解决方案如下:

  BoxLayout: 
       size_hint_y: None 
       height: sized_label.height 
       Label: 
        id: sized_label 
        text: "[b]CurrentHistory:[/b]\\nHeader:" 
        size_hint: (None, None) 
        markup: True 
        size: self.texture_size 
       Label: 

我正在将一个Label放在BoxLayout中。我将尺寸高度设置为包含文字的标签尺寸。带有文本的标签的大小与其文本的大小相同。由于带有文本的标签不像BoxLayout那么宽,所以我使用另一个标签来填充空白区域。

这是您的更新代码。我把它全部放在.py文件中,因为你在评论中说过你正在努力。还有很多我删除的代码,因为它是双重或不必要的。我希望现在让你继续工作更容易。我还为你的TextInput使用了类似text_hint的东西。如果你不希望出现这种情况应该很容易恢复这些更改...

这里是我更新的代码:

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.factory import Factory 

from kivy.uix.label import Label 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.textinput import TextInput 

from kivy.base import Builder 

kv_string = Builder.load_string(""" 
BoxLayout: 
    BoxLayout: 
     canvas: 
      Color: 
       rgba: 150/255, 150/255, 150/255, 1 
      Rectangle: 
       pos: self.pos 
       size: self.size 

     ScrollView: 
      id: scrlv 

      GridLayout: 
       cols: 1 
       size_hint_y: None 
       height: max(self.minimum_height, scrlv.height) 

       BoxLayout: 
        size_hint_y: None 
        height: sized_label.height 
        Label: 
         id: sized_label 
         text: "[b]CurrentHistory:[/b]\\nHeader:" 
         size_hint: (None, None) 
         markup: True 
         size: self.texture_size 
        Label: 

       TextInput: 
        size_hint_y: None 
        height: 80 
        hint_text: 'Enter Text Here' 
       Label: 

    BoxLayout: 
     orientation: "vertical" 
     size_hint_x: .25 

     canvas: 
      Color: 
       rgba: 240/255, 180/255, 80/255, 1 
      Rectangle: 
       pos: self.pos 
       size: self.size 

     Label: 
      text: "Label 1" 
     Label: 
      text: "Label 2" 
""") 



class MyApp(App): 

    def build(self): 
     return kv_string 



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

enter image description here

+0

问题不在于我无法让标签按照我的意愿工作,而是通过kv文件做出的结果给我想要的结果,而通过python来做并不是。在我的演示文件中,SegmentLayout与在kv文件中定义的网格布局姐妹相同。唯一的区别是SegmentLayout是在python vs kv文件中定义的。 – Hangman4358

+0

我在发布的kv文件中添加了SegmentLayout。当我编辑这个问题时,我直接将我的python文件和我的kv文件复制到原始文章中。我在哪里错过':'?我提供的演示显示段布局的行为与完全相同的kv定义的结构不同,我对此感到困惑 – Hangman4358