如何在 kv 文件中显示变量
How do you display a variable in a kv file
这是py文件
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen
from kivy.properties import ListProperty
from kivy.lang import Builder
from kivy.properties import BooleanProperty, NumericProperty
from kivy.uix.togglebutton import ToggleButton
import string
import random
class MyGrid(Widget):
name = ObjectProperty(None)
email = ObjectProperty(None)
bmi = NumericProperty(0)
def btn(self):
height = float(self.name.text)
weight = float(self.email.text)
bmi = weight/(height*height)
print("Name:", self.name.text, "email:", self.email.text, "bmi:", str(bmi))
self.name.text = ""
self.email.text = ""
show_popup()
class P(FloatLayout):
pass
class MyApp(App):
def build(self):
return MyGrid()
def show_popup():
show = P()
popupWindow = Popup(title="BMI", content=show, size_hint=(None,None),size=(400,400))
popupWindow.open()
if __name__ == "__main__":
MyApp().run()
这是kv文件
<MyGrid>:
name: name
email: email
GridLayout:
cols:1
size: root.width - 200, root.height -200
pos: 100, 100
GridLayout:
cols:2
Label:
text: "Height: "
TextInput:
id: name
multiline:False
Label:
text: "Weight: "
TextInput:
id: email
multiline:False
Button:
text:"Submit"
on_release: root.btn()
<P>:
Label:
text: "Your BMI is: "
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
如何在标签中显示变量'BMI'的值。我希望弹出消息显示以下文本:“您的 BMI 是:”+ bmi 但是当我这样做时会出现错误。如何在 kv 文件中显示变量。这是一个非常简单的问题,但我似乎无法在任何地方找到答案。你只是说文本:“你的 BMI 是:”+ root.bmi 因为我已经尝试过但没有用。
一种方法是将 BMI 传递给 show()
方法。这是一个修改后的 show()
方法:
def show_popup(bmi):
show = P()
popupWindow = Popup(title="BMI", content=show, size_hint=(None, None), size=(400, 400))
show.ids.bmi.text = "Your BMI is: " + str(bmi)
popupWindow.open()
以上需要kv
中的id
:
<P>:
Label:
id: bmi
text: "Your BMI is: "
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
并对btn()
传递BMI值的方法稍作修改:
def btn(self):
height = float(self.name.text)
weight = float(self.email.text)
bmi = weight / (height * height)
print("Name:", self.name.text, "email:", self.email.text, "bmi:", str(bmi))
self.name.text = ""
self.email.text = ""
show_popup(bmi)
这是py文件
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen
from kivy.properties import ListProperty
from kivy.lang import Builder
from kivy.properties import BooleanProperty, NumericProperty
from kivy.uix.togglebutton import ToggleButton
import string
import random
class MyGrid(Widget):
name = ObjectProperty(None)
email = ObjectProperty(None)
bmi = NumericProperty(0)
def btn(self):
height = float(self.name.text)
weight = float(self.email.text)
bmi = weight/(height*height)
print("Name:", self.name.text, "email:", self.email.text, "bmi:", str(bmi))
self.name.text = ""
self.email.text = ""
show_popup()
class P(FloatLayout):
pass
class MyApp(App):
def build(self):
return MyGrid()
def show_popup():
show = P()
popupWindow = Popup(title="BMI", content=show, size_hint=(None,None),size=(400,400))
popupWindow.open()
if __name__ == "__main__":
MyApp().run()
这是kv文件
<MyGrid>:
name: name
email: email
GridLayout:
cols:1
size: root.width - 200, root.height -200
pos: 100, 100
GridLayout:
cols:2
Label:
text: "Height: "
TextInput:
id: name
multiline:False
Label:
text: "Weight: "
TextInput:
id: email
multiline:False
Button:
text:"Submit"
on_release: root.btn()
<P>:
Label:
text: "Your BMI is: "
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
如何在标签中显示变量'BMI'的值。我希望弹出消息显示以下文本:“您的 BMI 是:”+ bmi 但是当我这样做时会出现错误。如何在 kv 文件中显示变量。这是一个非常简单的问题,但我似乎无法在任何地方找到答案。你只是说文本:“你的 BMI 是:”+ root.bmi 因为我已经尝试过但没有用。
一种方法是将 BMI 传递给 show()
方法。这是一个修改后的 show()
方法:
def show_popup(bmi):
show = P()
popupWindow = Popup(title="BMI", content=show, size_hint=(None, None), size=(400, 400))
show.ids.bmi.text = "Your BMI is: " + str(bmi)
popupWindow.open()
以上需要kv
中的id
:
<P>:
Label:
id: bmi
text: "Your BMI is: "
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
并对btn()
传递BMI值的方法稍作修改:
def btn(self):
height = float(self.name.text)
weight = float(self.email.text)
bmi = weight / (height * height)
print("Name:", self.name.text, "email:", self.email.text, "bmi:", str(bmi))
self.name.text = ""
self.email.text = ""
show_popup(bmi)