我正在尝试开发一个最终能够得分的应用程序。我要解决的一个问题是如何以某种方式按下或更新按钮后的文本。我是python / kivy的新手。我发现了几个类似的问题,涉及函数等,但是没有一个涉及.kv文件和单独的窗口。我的代码在下面。
main.py
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class MainWindow(Screen):
pass
class ScoreWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("mainkv.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
mainkv.kv
WindowManager:
MainWindow:
ScoreWindow:
<MainWindow>:
name: "main"
GridLayout:
cols: 1
Button:
text: "Quick Score"
on_release:
app.root.current = "score"
root.manager.transition.direction = "left"
<ScoreWindow>:
name: "score"
GridLayout:
cols: 7
Button:
name: "three"
text: "Press to Update"
on_release:
three.text = "Updated"
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: "Score"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: "Go Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
答案 0 :(得分:1)
您的Button
可以使用self
关键字在kvlang中进行引用。因此,只需在self.text = 'foo'
的{{1}}区域中添加另一行,写上on_release:
。