我正在用 Kivy 编写 Python 脚本。截至目前,该应用程序正在按预期运行。但我遇到了障碍并且无法找到解决方案。在“class Output(screen)”类中,我想提取用户将从“Class Cost(Screen)”和“Class Tip(Screen)”类中输入的屏幕输入生成的值。现在,作为编写代码后,来自屏幕的输入被拉入相应的类(成本和提示)并用 Python 打印。所以,我知道逻辑正在工作。我需要一种方法来调用类输出(屏幕)中的这些值). 预先感谢您的任何建议。
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
class Cost(Screen):
cost = ObjectProperty(None)
def btn(self):
cost = self.ids.cost.text
print(cost)
class Tip(Screen):
tip = ObjectProperty(None)
def btn(self):
tip = self.ids.tip.text
print(tip)
class Output(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("att.kv")
class ATT(App):
def build(self):
return kv
if __name__ == "__main__":
ATT().run()
type here
att.kv 文件
<Label>:
color:0.5,0.5,0.5,1
font_size:30
<Button>:
background_color: 0, 0, 3, .6
color:1.0,1.0,1.0,1
font_size:20
markup: True
WindowManager
Cost:
Tip:
Output:
<Cost>
name: "Cost"
cost: cost
Label:
size_hint:{0.3, 0.2}
pos_hint: {"x":0.20, "y":0.5}
color:1.0,1.0,1.0,1
text: "What's the Total Bill? $"
TextInput:
focus: "True"
id: cost
size_hint:{0.2, 0.1}
pos_hint: {"x":0.58, "y":0.55}
multiline:False
input_filter: "float"
Button:
size_hint:{0.2, 0.1}
pos_hint: {"x":0.4, "y":0.2}
text: "[b]Next[/b]"
on_press: root.btn()
on_release:
app.root.current = "Tip"
root.manager.transition.direction = "left"
<Tip>
name: "Tip"
tip: tip
Label:
size_hint:{0.3, 0.2}
pos_hint: {"x":0.24, "y":0.5}
color:1.0,1.0,1.0,1
text: "Tip Percentage? %"
TextInput:
id: tip
size_hint:{0.2, 0.1}
pos_hint: {"x":0.58, "y":0.55}
multiline:False
input_filter: "float"
Button:
size_hint:{0.2, 0.1}
pos_hint: {"x":0.5, "y":0.2}
text: "[b]Next[/b]"
on_press: root.btn()
on_release:
app.root.current = "Output"
root.manager.transition.direction = "left"
Button:
size_hint:{0.2, 0.1}
pos_hint: {"x":0.27, "y":0.2}
text: "[b]Back[/b]"
on_release:
app.root.current = "Cost"
root.manager.transition.direction = "right"
<Output>
name: "Output"
Label:
size_hint:{0.3, 0.2}
pos_hint: {"x":0.20, "y":0.5}
color:1.0,1.0,1.0,1
text: "Output"
Button:
size_hint:{0.2, 0.1}
pos_hint: {"x":0.5, "y":0.2}
text: "[b]Close[/b]"
on_press: app.stop()
Button:
size_hint:{0.2, 0.1}
pos_hint: {"x":0.27, "y":0.2}
text: "[b]Start Over[/b]"
on_press:
app.root.current = "Cost"
root.manager.transition.direction = "right"
如果将
id
添加到 Output
类中:
<Output>
name: "Output"
Label:
id: output
size_hint:{0.3, 0.2}
然后你可以在
on_enter()
类中添加 Output
方法:
class Output(Screen):
def on_enter(self, *args):
self.ids.output.text = 'Cost: ' + self.manager.get_screen('Cost').ids.cost.text + ' Tip: ' + self.manager.get_screen('Tip').ids.tip.text
显然,您可以调整上面的代码来进行任何您想要的计算。