0

我正在尝试开发一个自定义小部件,它可以在运行时添加到基本小部件中,并将适应窗口大小的变化。

基于多个答案,我使用了 gridlayout 的 minimum_height 属性来根据其子标签中的 texture_size 调整 GridLayout 的高度。

问题:当窗口宽度减小时,标签和网格高度会相应调整,但是当窗口宽度恢复到原来的值时,minimum_height 属性仍然保持在窗口宽度减小时的值。

main_2.py

    from kivy.app import App
    from kivy.uix.gridlayout import GridLayout
    from kivy.lang import Builder

    Builder.load_file('mcq2_2.kv')

    class MCQ2(GridLayout):

            def __init__(self,s_group,s_Q,s_A1,s_A2,**kwargs):
                    super(MCQ2,self).__init__(**kwargs)
                    self.ids.mcq2_A1.group = s_group
                    self.ids.mcq2_A2.group = s_group
                    self.ids.mcq2_A1.text = s_A1
                    self.ids.mcq2_A2.text = s_A2
                    self.ids.mcq2_Q.text = s_Q


    class ScrollBaseWidget(GridLayout):
            pass

    class Mcq2App(App):
            def build(self):
                    BW = ScrollBaseWidget()

                    Q1 = MCQ2(s_group='g1',s_Q='q1'*50,s_A1='A1_1',s_A2='A2_1')
                    BW.add_widget(Q1)

                    return BW

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

mcq2_2.kv

<MCQ2>:
    cols:3
    size_hint: 1, None
        height: max(40, self.minimum_height)
        
    on_height: print('on_height--> self.size', self.size)
    canvas:
        Color:
                rgba: 0.1,0.8,0.8,1
        Rectangle:
                size: root.size
                pos: root.pos
    Label:
        id: mcq2_Q
        size_hint: None, None
        size: root.width - 230, self.texture_size[1]
        on_size: print(self.text[:1], 'on_size --> texture_size=', self.texture_size, 'label_size=', self.size, 'minimum_height=' , root.minimum_height)
        text_size: self.width , None
        on_texture_size: print(self.text[:1], 'on_texture_size --> texture_size=', self.texture_size, 'label_size=', self.size, 'minimum_height=' , root.minimum_height)
        
    CheckBox:
        id: mcq2_A1

    CheckBox: 
        id: mcq2_A2
        
<ScrollBaseWidget@GridLayout>:       
    cols: 1
    size_hint_y: None
    height: self.minimum_height
4

0 回答 0