首页 > 解决方案 > Kivy:GridLayout 中的按钮有间隙

问题描述

在 GridLayout 内部,不会出现奇数 Button 子项。

我已经尝试了 Button 和 GridLayout 大小、size_hint、高度等的几种配置,但似乎无法解决这个问题。从子项中删除 Button 类可以解决此问题,但我想要按钮小部件的功能。

主要.py:

from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.gridlayout import GridLayout
from kivy.properties import NumericProperty, StringProperty, ListProperty
from kivy.uix.behaviors.focus import FocusBehavior
from kivy.uix.button import Button


class Tube(Button, RelativeLayout, FocusBehavior):
    pass


class TubeMapView(GridLayout, FocusBehavior):
    orderNumber = NumericProperty()
    customerName = StringProperty("")
    tubeList = ListProperty([])
    bundleList = ListProperty([])

    def add_tube(self):
        self.tubeList.append(Tube())
        self.add_widget(self.tubeList[-1])

    def _on_focusable(self, instance, value):
        self.add_tube()

    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        print(keycode)
        if keycode[1] is 'enter':
            self.add_tube()




class LengthView(GridLayout):
    pass


class AppView(GridLayout):
    pass


class TubeMapApp(App):
    pass


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

管图.kv:

<Tube>:
    size_hint_y: None
    height: dp(60)
    canvas.before:
        Color:
            rgba: (0,1,0,1)
        Rectangle:
            size: self.size

<LengthView>:
    size_hint_x: 1
    size_hint_y: 1

    canvas.before:
        Color:
            rgba: (0,0,1,1)
        Rectangle:
            size: self.size


<TubeMapView>:
    cols: 1
    rows: None
    size_hint_max_x:
    size_hint_y: None
    height: self.minimum_height
    canvas.before:
        Color:
            rgba: (0,1,0,1)
        Rectangle:
            pos: self.pos
            size: self.size


AppView:
    cols: 2
    rows: None
    RelativeLayout:
        size_hint_x: 0.75
        ScrollView:
            size: self.size
            TubeMapView:
                focus: True
                Tube:
                Tube:
                Tube:
    RelativeLayout:
        size_hint_x: 0.25
        ScrollView:
            LengthView:

我希望每个都渲染,但只有其他的 Button 渲染,从第一个开始:

渲染不良的 GridLayout

标签: pythonkivy

解决方案


如果您希望自己TubeRelativeLayout行为类似于 a Button,请将您的类的声明更改Tube为:

class Tube(ButtonBehavior, FocusBehavior, RelativeLayout):
    pass

请注意,文档说 Behavior 类必须Widget在继承中的类之前。

这种改变符合我的想法。


推荐阅读