在 Python 中显示 属性 沮丧
Display property in Python Kivy
我一直在观看 Kivy 和 Python 关于创建计算器的教程,但我看到了这个 属性:显示小部件 ID 的值。使用此值,它可以访问小部件的其他属性。
这是代码(.kv 文件):
<CalcGridLayout>:
id: calculator
display: entry #this is the display property
rows: 5
padding: 10
spacing: 10
BoxLayout:
TextInput:
id: entry #with the value of this
font_size: 32
multiline: False
BoxLayout:
spacing: 10
Button:
text: "7"
on_press: entry.text += self.text
Button:
text: "8"
on_press: entry.text += self.text
Button:
text: "9"
on_press: entry.text += self.text
Button:
text: "+"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "4"
on_press: entry.text += self.text
Button:
text: "5"
on_press: entry.text += self.text
Button:
text: "6"
on_press: entry.text += self.text
Button:
text: "-"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "1"
on_press: entry.text += self.text
Button:
text: "2"
on_press: entry.text += self.text
Button:
text: "3"
on_press: entry.text += self.text
Button:
text: "*"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "AC"
on_press: entry.text = ""
Button:
text: "0"
on_press: entry.text += self.text
Button:
text: "="
on_press: calculator.calculate(entry.text)
Button:
text: "/"
on_press: entry.text += self.text
那是什么variable/property?它有什么用?
参考你的kv文件:
calc.kv
display: entry
main.py
display = ObjectProperty(None)
display - 是您的变量,它被声明为 Kivy ObjectProperty。它用于引用在 kv 文件中实例化的 TextInput 子控件。在声明一个 ObjectProperty 之后,您将它连接到在 kv 文件中创建的子部件,例如display: entry
。完成后,您可以轻松地在 calculate 方法中引用 TextInput 属性。
示例
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
class CalcGridLayout(GridLayout):
display = ObjectProperty(None)
def calculate(self, dt):
print(self.display.text)
class CalcApp(App):
def build(self):
return CalcGridLayout()
if __name__ == '__main__':
CalcApp().run()
calc.kv
#:kivy 1.10.0
<CalcGridLayout>:
id: calculator
display: entry #this is the display property
rows: 5
padding: 10
spacing: 10
BoxLayout:
TextInput:
id: entry #with the value of this
font_size: 32
multiline: False
BoxLayout:
spacing: 10
Button:
text: "7"
on_press: entry.text += self.text
Button:
text: "8"
on_press: entry.text += self.text
Button:
text: "9"
on_press: entry.text += self.text
Button:
text: "+"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "4"
on_press: entry.text += self.text
Button:
text: "5"
on_press: entry.text += self.text
Button:
text: "6"
on_press: entry.text += self.text
Button:
text: "-"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "1"
on_press: entry.text += self.text
Button:
text: "2"
on_press: entry.text += self.text
Button:
text: "3"
on_press: entry.text += self.text
Button:
text: "*"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "AC"
on_press: entry.text = ""
Button:
text: "0"
on_press: entry.text += self.text
Button:
text: "="
on_press: calculator.calculate(entry.text)
Button:
text: "/"
on_press: entry.text += self.text
我一直在观看 Kivy 和 Python 关于创建计算器的教程,但我看到了这个 属性:显示小部件 ID 的值。使用此值,它可以访问小部件的其他属性。 这是代码(.kv 文件):
<CalcGridLayout>:
id: calculator
display: entry #this is the display property
rows: 5
padding: 10
spacing: 10
BoxLayout:
TextInput:
id: entry #with the value of this
font_size: 32
multiline: False
BoxLayout:
spacing: 10
Button:
text: "7"
on_press: entry.text += self.text
Button:
text: "8"
on_press: entry.text += self.text
Button:
text: "9"
on_press: entry.text += self.text
Button:
text: "+"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "4"
on_press: entry.text += self.text
Button:
text: "5"
on_press: entry.text += self.text
Button:
text: "6"
on_press: entry.text += self.text
Button:
text: "-"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "1"
on_press: entry.text += self.text
Button:
text: "2"
on_press: entry.text += self.text
Button:
text: "3"
on_press: entry.text += self.text
Button:
text: "*"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "AC"
on_press: entry.text = ""
Button:
text: "0"
on_press: entry.text += self.text
Button:
text: "="
on_press: calculator.calculate(entry.text)
Button:
text: "/"
on_press: entry.text += self.text
那是什么variable/property?它有什么用?
参考你的kv文件:
calc.kv
display: entry
main.py
display = ObjectProperty(None)
display - 是您的变量,它被声明为 Kivy ObjectProperty。它用于引用在 kv 文件中实例化的 TextInput 子控件。在声明一个 ObjectProperty 之后,您将它连接到在 kv 文件中创建的子部件,例如display: entry
。完成后,您可以轻松地在 calculate 方法中引用 TextInput 属性。
示例
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
class CalcGridLayout(GridLayout):
display = ObjectProperty(None)
def calculate(self, dt):
print(self.display.text)
class CalcApp(App):
def build(self):
return CalcGridLayout()
if __name__ == '__main__':
CalcApp().run()
calc.kv
#:kivy 1.10.0
<CalcGridLayout>:
id: calculator
display: entry #this is the display property
rows: 5
padding: 10
spacing: 10
BoxLayout:
TextInput:
id: entry #with the value of this
font_size: 32
multiline: False
BoxLayout:
spacing: 10
Button:
text: "7"
on_press: entry.text += self.text
Button:
text: "8"
on_press: entry.text += self.text
Button:
text: "9"
on_press: entry.text += self.text
Button:
text: "+"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "4"
on_press: entry.text += self.text
Button:
text: "5"
on_press: entry.text += self.text
Button:
text: "6"
on_press: entry.text += self.text
Button:
text: "-"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "1"
on_press: entry.text += self.text
Button:
text: "2"
on_press: entry.text += self.text
Button:
text: "3"
on_press: entry.text += self.text
Button:
text: "*"
on_press: entry.text += self.text
BoxLayout:
spacing: 10
Button:
text: "AC"
on_press: entry.text = ""
Button:
text: "0"
on_press: entry.text += self.text
Button:
text: "="
on_press: calculator.calculate(entry.text)
Button:
text: "/"
on_press: entry.text += self.text