下面是我现在要做的,我创建了一个可以动态创建按钮的函数(在boxlayout中):
def create_class_button(self, size_hint_x:float, text:str, text_color:tuple, background_color:tuple) -> TextBoxLayout:
# Ignore this box
box = TextBoxLayout(size_hint_x=size_hint_x, padding=(30,6.5), on_press=self.list_item_pressed, background_color=(0,0,0,0))
# RoundedButton inherited from Button
btn = RoundedButton(text=text, on_press=self.list_item_pressed, color=text_color, background_color = (0,0,0,0))
with btn.canvas.before:
Color(rgba=background_color)
Rectangle(size=btn.size, pos=btn.pos)
box.add_widget(btn)
return box
画布始终在点(0,0)处绘制
我如何让它跟随我的RoundedButton
位置和大小
在多次阅读您的问题之后,我想您是在问如何让
Rectangle
跟随您的RoundedButton
。最好的方法是在kv
中为RoundedButton
创建一个规则:在python中也可以这样做,但是必须设置绑定以使
Rectangle
跟随RoundedButton
。kv
语言为您设置这些绑定另外,请注意
Widget
的pos
和size
最初设置为默认值(0,0)
和(100,100)
,并且在绘制Widget
时更新。因此,您的画布正在使用这些初始默认值。由于没有设置绑定,所以当Widget
的pos
和size
更改时,画布不会更改相关问题 更多 >
编程相关推荐