我想在 Kivy 中用红色矩形覆盖一个按钮。此按钮位于BoxLayout. 我已经定义MyButton了在按钮上绘制矩形的类,它可以工作。但是,如果我固定BoxLayout按钮所在的宽度,矩形会转到左下角。MWE:# -*- coding: utf-8 -*-from kivy.app import Appfrom kivy.uix.boxlayout import BoxLayoutfrom kivy.lang import Builderfrom kivy.uix.button import Buttonfrom kivy.graphics import Color, RectangleBuilder.load_string('''<MainApp>: orientation: 'horizontal' BoxLayout: orientation: 'vertical' # width: 100 # uncommenting these two lines breaks the rectangle positioning # size_hint: (None,1) MyButton: text: 'button1' size_hint: (1,None) height: 50 Label: Label:''')class MyButton(Button): def __init__(self, **kwargs): super(MyButton, self).__init__(**kwargs) def on_size(self, *args): self.canvas.after.clear() with self.canvas.after: Color(1, 0, 0, 1) Rectangle(pos=self.pos, size=self.size)class MainApp(BoxLayout): passclass TestApp(App): def build(self): return MainApp()if __name__ == '__main__': TestApp().run()取消注释中的两条注释行Builder.load_string会破坏矩形定位。我错过了什么?
1 回答
www说
TA贡献1775条经验 获得超8个赞
在你的MyButton
课堂上,你有一个on_size()
方法,但没有on_pos()
方法。我认为您只需要添加该方法:
def on_pos(self, *args): self.on_size()
添加回答
举报
0/150
提交
取消