【问题标题】:KIVY : Image + Label inside dynamic buttonsKIVY:动态按钮内的图像+标签
【发布时间】:2014-08-09 04:28:41
【问题描述】:

我想在 for 循环中创建的动态按钮中显示 Image+label。问题是它仅在最后一个按钮中显示图像+标签布局。如何在所有按钮中显示它?

main.py

class HomeScreen(Screen):
    grid_l = ObjectProperty(None)
    top_lbl = ObjectProperty(None)

    def search_btn_pressed(self):
        grid = self.grid_l
        grid.bind(minimum_height=grid.setter('height'),
                     minimum_width=grid.setter('width'))

        for i in range(3):
                layout = GridLayout(cols=1)
                print layout
                img = Image(source='kivy.png')
                print img
                lbl = Label(text='label')
                layout.add_widget(img)
                layout.add_widget(lbl)

                btn1 = Button(size_hint=(1, None))
                btn1.text = '%r' % i
                btn1.add_widget(layout)

                grid.add_widget(btn1)

.kv

#:kivy 1.7.2

<HomeScreen>:
    scroll_view: scrollviewID
    top_lbl: lblID
    grid_l: gridlayoutID
    AnchorLayout:
        size_hint: 1, .1   
        pos_hint: {'x': 0, 'y': .9}
        anchor_x: 'center'
        anchor_y: 'center'
        Label:
            id: lblID
            text: 'Button Tester'
    Button:
        size_hint: 1, .1   
        pos_hint: {'x': 0, 'y': .8}
        text: 'Press me!'
        on_release: root.search_btn_pressed()
    ScrollView:
        id: scrollviewID
        orientation: 'vertical'
        pos_hint: {'x': 0, 'y': 0}
        size_hint: 1, .8
        bar_width: '8dp'
        GridLayout:
            id: gridlayoutID
            cols: 1
            size_hint: 1, None
            row_default_height: 40
            row_force_default: False

【问题讨论】:

    标签: android python kivy


    【解决方案1】:

    它实际上不仅显示最后一个按钮 - 它显示每个按钮,而是显示在相同的位置。问题是 Button 不是布局,因此不执行其子级的布局。因此,每个按钮的GridLayout 呈现在0, 0,大小为100, 100,相对于最近的相对父级的原点(在这种情况下,GridLayoutgrid_l 因为它包含在ScrollView 中) .

    当您将小部件添加到非布局小部件时,您可以通过设置位置和大小来布局这些小部件。请注意,您必须设置实际的pos(或xy)和size(或widthheight) - 你不能使用pos_hintsize_hint,因为它们只是由布局处理。

    这可以在kv language 中使用dynamic classes 轻松完成:

    <CustomButton@Button>:
        image_source: ''
        subtext: ''
        GridLayout:
            height: self.parent.height  # match the button's height
            width: 100                  # set to whatever you would like
            pos: self.parent.pos        # match the button's position
            cols: 1
            Image:
                source: root.image_source
            Label:
                text: root.subtext
    

    要使用您的动态类,您需要导入Factory

    from kivy.factory import Factory
    

    然后,在你的循环中:

    for i in range(3):
        btn = Factory.CustomButton(text=str(i), size_hint=(1, None),
                                   image_source='kivy.png', subtext='label')
        grid.add_widget(btn)
    

    最后,附注:每次调用 search_btn_pressed() 时,您都会在 grid_l 上创建新绑定。这些绑定只能创建一次。您可以通过将这些绑定移动到 HomeScreen.__init__() 在 Python 中绑定一次,但同样,这在 kv 中更容易:

        GridLayout:
            id: gridlayoutID
            cols: 1
            size_hint: 1, None
            row_default_height: 40
            row_force_default: False
            height: self.minimum_height   # bind height to minimum_height
            width: self.minimum_width     # bind width to minimum_width
    

    【讨论】:

      猜你喜欢
      • 2019-05-12
      • 1970-01-01
      • 2014-02-16
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 2016-05-24
      相关资源
      最近更新 更多