我一直在摆弄Kivy语言并做了一些搜索,但还没找到适合我情况的解决方案。我想我在这里缺少一点基本的东西。
我试图通过按下按钮调用屏幕内的功能,我想用kv文件来促进这个功能。我简化了代码并省略了kv文件中的一些格式,例如布局和按钮大小等。
“输入”变量在屏幕上显示为“未输入”,但是当我按下按钮时,标签不会改变,并且没有输入功能,没有任何反应。
的Python:
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.screenmanager import Screenmanager, Screen
from kivy.properties import StringProperty
class ScreenManager(ScreenManager):
pass
class StartMenu(Screen):
pass
class MyScreen(Screen):
entered = StringProperty()
entered = "Not Entered"
def my_function(self, *args):
self.entered = "Entered"
class MyApp(App):
def build(self):
return ScreenManager()
if __name__ == "__main__":
MyApp().run()
Kivy:my.kv
#:kivy 1.9.1
<ScreenManager>:
StartMenu:
MyScreen:
<StartMenu>:
name: 'StartMenu'
Button:
on_release:
root.manager.current = 'MyScreen'
<MyScreen>:
name: 'MyScreen'
Label:
text: root.entered
Button:
on_release:
root.my_function()
谢谢你的时间!
答案 0 :(得分:1)
问题在于:
class MyScreen(Screen):
entered = StringProperty()
entered = "Not Entered"
entered
会立即被覆盖(并且是标准的类属性,会丢失所有的事件魔法)。而是将其初始化为entered = StringProperty("Not Entered")
,或在kv
文件中将其初始化为
<MyScreen>:
entered: "Not Entered"
顺便说一下,为了让你的例子有用,应该有某种布局:
#:kivy 1.9.1
<ScreenManager>:
StartMenu:
MyScreen:
<StartMenu>:
name: 'StartMenu'
Button:
on_release:
root.manager.current = 'MyScreen'
<MyScreen>:
entered:"Not Entered"
name: 'MyScreen'
GridLayout:
cols: 2
Label:
text: root.entered
Button:
on_release:
root.my_function()