【问题标题】:Kivy align Switch left in BoxLayoutKivy在BoxLayout中向左对齐开关
【发布时间】:2020-06-29 16:15:07
【问题描述】:

我想将开关对齐到 BoxLayout 的左侧。对于标签,我使用以下代码实现了这一点:

text_size: self.size

这会将我的标签文本放在我的 boxlayout 的左下角。但是,我无法使用 switch 小部件 来做同样的事情。我尝试使用 size_hint_x、size、pos 等,但我无法在不影响框大小的情况下正确对齐元素。 目前我的标签已正确对齐,因此我尝试为它们分配 id 并根据标签的当前位置定位开关,如下所示:

BoxLayout:
    padding: 100, 0, 0, 0
    orientation: 'horizontal'
    text_size: self.size
    valign: 'middle'
    Label:
        text: 'this is already correctly aligned'
        id: 'labelCorrectlyAligned'
#Some other code
BoxLayout:
    padding: 100, 0, 0, 0
    orientation: 'horizontal'
    #here i need something like text_size: self.size but for switches
    Switch:
        size_hint_x: labelCorrectlyAligned.pos[0] #this should be the current X-position of the label
        #pos_hint_x: labelCorrectlyAligned.pos[0] #didnt work either

【问题讨论】:

    标签: python switch-statement position kivy alignment


    【解决方案1】:

    如果我正确理解您的问题,您可以将Switchsize 设置为documentation 中所述的最小值:

    要求的最小尺寸为 83x32 像素

    任何大于该尺寸的尺寸都只会使 83 x 32 图像以该较大尺寸居中。

    此外,在这些布局情况下,我发现为Widgets 的背景着色有助于轻松查看它们的确切位置和大小。这是您的“kv”的修改版本,它可以执行上述两个建议:

    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            padding: 100, 0, 0, 0
            orientation: 'horizontal'
            canvas.before:
                Color:
                    rgba: 1,0,0,1
                Rectangle:
                    pos: self.pos
                    size: self.size
            Label:
                text: 'this is already correctly aligned'
                text_size: self.size
                valign: 'middle'
                id: 'labelCorrectlyAligned'
                canvas.before:
                    Color:
                        rgba: 0,1,0,1
                    Rectangle:
                        pos: self.pos
                        size: self.size
    
    # Some other code
    
        BoxLayout:
            padding: 100, 0, 0, 0
            orientation: 'horizontal'
            canvas.before:
                Color:
                    rgba: 1,1,0,1
                Rectangle:
                    pos: self.pos
                    size: self.size
            Switch:
                size_hint: None, None
                size: 83, 32
                on_active: app.setBluetoothConnection(self)
                canvas.before:
                    Color:
                        rgba: 0,0,1,1
                    Rectangle:
                        pos: self.pos
                        size: self.size
    

    当然,当您对布局感到满意时,只需删除 canvas.before: 块即可。

    顺便说一句,text_sizevalignBoxLayout 中没有作用,它们必须在Label 规则中。

    所以,SwitchLabel 都在 BoxLayout 的左侧。调整Label 大小的常用方法是使用:

    size_hint: None, None
    size: self.texture_size
    

    这为您提供Label 的最小大小。

    【讨论】:

    • 啊,当然,谢谢!在我不得不用占位符框和东西包裹东西之前,这让它变得如此简单和干净。谢谢!
    【解决方案2】:

    编辑:为方便起见,我为左对齐开关创建了一个动态类:

    <SwitchL@Switch>:
        size_hint_x: None
        size: 83, self.height
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 2015-09-28
      • 2012-07-28
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多