我正在尝试开发一个自定义窗口小部件,可以在运行时添加到基本窗口小部件,并将适应窗口大小的变化。
根据多个答案,我使用了gridlayout的minimum_height属性,根据子标签中的texture_size调整GridLayout的高度。
问题:当窗口宽度减小时,标签和网格高度会相应调整,但当窗口宽度恢复原始时,minimum_height属性仍然保持窗口宽度减小时的值。
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>:
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