我是新手,但是我想通过单击图像来更改屏幕。我使用了ButtonBehavior并调用了我的类ImageButton的on_press方法,但是我无法弄清楚要放置什么代码。我在kivy文件上尝试了on_press:screen_manager.current ='window1',但无法正常工作
Python代码
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
class Window1(Screen):
pass
class Window2(Screen):
pass
class WindowManager(ScreenManager):
pass
class ImageButton(ButtonBehavior, Image):
def on_press(self):
# what to call
class Phone(FloatLayout):
pass
class MyApp(App):
def build(self):
return Phone()
if __name__ == '__main__':
MyApp().run()
kv文件
<Phone>:
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
WindowManager:
id: screen_manager
size_hint: 1, 0.9
anchor_y: 'top'
transition: FadeTransition()
Window1:
Window2:
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
BoxLayout:
canvas:
Color:
rgba: 228, 241, 254, 1
Rectangle:
size: self.size
orientation: 'horizontal'
size_hint: 1, .1
ImageButton:
source: 'pic1.png'
on_press: self.on_press()
ImageButton:
source: 'pic2.png'
on_press: self.on_press()
<Window1>:
name: 'window1'
Label:
text: 'Window1'
<Window2>:
name: 'window2'
Label:
text: 'Window2'
有人对此提供帮助。我应该在我的kv文件中缺少什么
on_press:app.root.ids._screen_manager.current ='window1'
这里是解释
当解析kv代码时,id字段进入称为id的字典,该字典存储指向小部件对象的指针。
每个基维规则都有一个单独的ID名称空间。
打破它:
app.root.ids.screen_manager
应用是您的应用
root是根小部件,电话
ids是在根级别定义的id的决定
Elliot Garbus的信用额