python - 使用 python/kivy 运行应用程序时仅显示“else 语句”
问题描述
我一直在使用 Kivy 开发一个应用程序,该应用程序接收 0、1 或 2 作为用户输入的文本,最终屏幕上的标签显示的文本应该根据接收到的输入而有所不同。它不再出现任何错误,但即使满足“if 语句”的要求,最终屏幕也只会显示“else”文本(“Bad Luck”)。
通过搜索类似的问题,我尝试使用整数的“input_filter”来解决问题,但它没有。
我是一个没有编程背景的初学者,所以请随意在你的答案中尽可能详细。提前感谢您的时间和您可能分享的任何想法/建议。以下是 .py 和 .kv 文件的代码:
.py 文件:
import kivy
from kivy.app import App
from kivy.lang import Builder
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.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty, StringProperty, NumericProperty
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
pass
class FourthWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class MyApp(App):
def build(self):
kv = Builder.load_file("my.kv")
return kv
if __name__=="__main__":
MyApp().run()
.kv 文件:
WindowManager:
MainWindow:
SecondWindow:
ThirdWindow:
FourthWindow:
<MainWindow>:
name: "main"
GridLayout:
cols:1
rows:2
Label:
text: "stuff"
Button:
text: "stuff"
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
GridLayout:
cols:1
rows:2
GridLayout:
cols:2
Label:
text: "stuff"
TextInput:
id: ti_a
multiline:False
Label:
text: "stuff"
TextInput:
id: ti_b
multiline:False
Label:
text: "stuff"
TextInput:
id: ti_c
multiline:False
Label:
text: "stuff"
TextInput:
id: ti_d
multiline:False
Label:
text: "stuff"
TextInput:
id: ti_e
multiline:False
GridLayout:
cols:2
Button:
text: "stuff"
on_release:
app.root.current = "third"
root.manager.transition.direction = "left"
Button:
text: "Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
<ThirdWindow>:
name: "third"
GridLayout:
cols:1
rows:2
GridLayout:
cols:2
Label:
text: "stuff"
TextInput:
id: ti_f
multiline:False
GridLayout:
cols:2
Button:
text: "stuff"
on_release:
app.root.current = "fourth"
root.manager.transition.direction = "left"
Button:
text: "Back"
on_release:
app.root.current = "second"
root.manager.transition.direction = "right"
<FourthWindow>:
name: "fourth"
Label:
text: "Stuff" if root.manager.get_screen("second").ids.ti_a.text == "0" and root.manager.get_screen("second").ids.ti_b.text == "0" and root.manager.get_screen("second").ids.ti_c.text == "0" and root.manager.get_screen("second").ids.ti_d.text == "0" and root.manager.get_screen("second").ids.ti_e.text == "1" and root.manager.get_screen("third").ids.ti_f.text == "0" else "Bad Luck"
解决方案
问题是 kivy 无法识别表达式:
root.manager.get_screen("second").ids.ti_a.text
作为它可以绑定的属性,因此Label
您的 text的值FourthWindow
仅被评估一次(在FourthWindow
创建时)。在您的应用程序中对另一个进行任何更改Labels
都不会影响FourthWindow
. 请参阅文档。
解决方法是编写您自己的代码,Label
通过添加一个id
来更新它Label
:
<FourthWindow>:
name: "fourth"
Label:
id: lab
text: ""
并添加一个on_pre_enter()
方法FourthWindow
:
class FourthWindow(Screen):
def on_pre_enter(self, *args):
if self.manager.get_screen("second").ids.ti_a.text == "0" and \
self.manager.get_screen("second").ids.ti_b.text == "0" and \
self.manager.get_screen("second").ids.ti_c.text == "0" and \
self.manager.get_screen("second").ids.ti_d.text == "0" and \
self.manager.get_screen("second").ids.ti_e.text == "1" and \
self.manager.get_screen("third").ids.ti_f.text == "0":
self.ids.lab.text = 'Stuff'
else:
self.ids.lab.text = 'Bad Luck'
这将评估您的表达式并在显示text
之前设置每次FourthWindow
。
推荐阅读
- c# - c# Azure IOT Edge Auto-provision with Device Provisioning Service(DPS) with x509 certificate sample required required
- php - ftp_rawlist 在被动模式的 FTPES 服务器上总是失败
- windows - 为什么用 TCL 打开文件可以在 LINUX 上工作,而不能在 Windows 上工作?
- mysql - 创建表时php mysql“唯一”错误
- pervasive - 如何访问 *.xq4 和 *.xq5 后缀文件?
- c# - 为没有无参数构造函数的类创建通用构建器模式
- vb.net - 在 VB.Net 中重命名数据集
- c# - 发送消息时如何解决 Azure EventHub 中的 SocketExceptions
- git - git:撤消上次提交并返回当前版本
- amazon-web-services - 如何在 Media Temple DV 上设置 AWS 凭证