据我从文档中所知:self
-对当前小部件实例的引用;root
-引用根小部件实例(顶部布局(
所以我有教程乒乓球应用程序。为什么我们在矩形参数中使用self
?self
是指当前的widget实例Rectangle
,所以当我们写size: 10, self.height
时,这不意味着矩形高度等于矩形高度吗?或者这个self
指的是什么实例?谢谢
代码.py:
from kivy.app import App
from kivy.uix.widget import Widget
class PongGame(Widget):
pass
class Grudget3App(App):
def build(self):
return PongGame()
Grudget3App().run()
代码kv:
<PongGame>
canvas:
Rectangle:
pos: self.center_x-5, 0
size: 10, self.height
Label:
font_size: 70
center_x: root.width / 4
top: root.top -50
text: "0"
Label:
font_size: 70
center_x: root.width * 3/4
top: root.top -50
text: "0"
self引用当前小部件实例Rectangle
矩形不是一个小部件,它是一个画布指令。self
确实引用了当前的小部件,在您的示例中它就是PongGame
。
self总是指当前小部件,如果您在画布中说size:self.size,它将取根小部件的大小。当你说root.something时,你指的是你的小部件的顶级属性,例如
main.kv
<BoxLayout>:
text: '' # now you can use this is python as a stringproperty to change textinput
TextInput:
text:root.text