简体   繁体   中英

is pos_hint incompatible with DragBehaviour in Kivy?

The documentation offers an example of how to use DragBehaviour which works as long as I use pos to indicate the place the widget, but not if I use pos_hint :

from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder

kv = '''
<DragLabel>:
    # Define the properties for the DragLabel
    drag_rectangle: self.x, self.y, self.width, self.height
    drag_timeout: 10000000
    drag_distance: 0

FloatLayout:
    DragLabel:

        ##################################
        ##### Comment this line and works!
        pos_hint: {'x': 0.5, 'y':0.5}


        size_hint: 0.25, 0.2
        text: 'Drag me'
'''

class DragLabel(DragBehavior, Label):
    pass
class TestApp(App):
    def build(self):
        return Builder.load_string(kv)
TestApp().run()

How can I use pos_hint with DragBehaviour ? Are they incompatible because of the way Kivy binds properties?

According to "Creating Apps In Kivy" by Dusty Phillips:

The Layout sets pos based on the pos_hint ; they aren't strictly independent values. The same can be said for size_hint , which is why layouts require you to explicitly set size_hint to None if you want to manipulate the size directly.

In other words, both the FloatLayout and the DragBehavior want to set the widget's pos , and because the layout gets the last go at it, it ends up winning. But FloatLayout sets the pos based on the pos_hint , and doesn't try to set it if the latter is uninitialized. Likewise, if you were to try to set the size , FloatLayout would use its default size_hint of (1,1) to overrule you, unless you first explicitly set size_hint to (None,None) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM