如何使用to_widget()使一些东西在散点图中保持静止

2024-10-03 11:15:11 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我想让某些元素相对于窗口保持ScatterPlane静止,在kv中,我可以做:

ScatterPlane:
    Label:
        text: 'Locked to Window'
        pos: -root.x, -root.y

但是,如果我尝试:

pos: root.to_widget(0, 0)

它不起作用。为什么会这样?你知道吗

下面的一些代码演示了这一点:

import kivy
kivy.require('1.9.0') 
from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.scatter import ScatterPlane 
from kivy.uix.relativelayout import RelativeLayout 
from kivy.lang import Builder 

presentation = Builder.load_string(""" 
<ScatterPlane>: 
    Label: 
        #pos: root.to_widget(0, 0) #doesnt work 
        pos: -root.x, -root.y      #works 
        width: root.width 
        text: 'Test: ' + str(root.pos) + ', ' + str(root.to_widget(0, 0))  

RelativeLayout: 
    ScatterPlane: 
""") 


class TestApp(App): 
    def build(self): 
        return presentation  

if __name__ == '__main__': 
    TestApp().run()

Tags: totextfromposimportappbuilderroot