Use custom widgets kivy

后端 未结 1 1175
庸人自扰
庸人自扰 2021-01-14 19:23

I\'m trying to build a kivy app using some custom widgets. However whenever I try to use them they never work with my layout. Using a normal button:

import         


        
相关标签:
1条回答
  • 2021-01-14 20:21

    Your actual problem is that although your MyWidget is placed in a BoxLayout in the kv file, its child BoxLayout does not have its size set to the MyWidget size, and therefore just maintains the default size and position of (100, 100) in the bottom left of the screen.

    You can fix this by giving it the extra size: root.size rule as you did within the <RootWidget> rule, or actually normally it's easier to just use a BoxLayout (i.e. subclass BoxLayout instead of Widget) which of course gives you the automatic resizing/positioning for free.

    Also, as Joran said, if you just want to do something when the button is pressed you can use the second method...that's what you're intended to do! I don't know what example you are looking at, but you wouldn't normally need a complex arrangement like yours.

    You might also be interested to know that in the most recent release (1.8) the button behavior has been abstracted to a ButtonBehavior class that handles detecting touches and dispatching on_press etc. appropriately. The Behavior isn't a widget, so you can subclass it with any other widget to make anything into a button!

    0 讨论(0)
提交回复
热议问题