如何通过刷猕猴桃来更改屏幕

时间:2020-06-20 16:17:37

标签: python android kivy

我正在尝试通过滑动屏幕来切换到另一个屏幕。我尝试过轮播,但似乎只适用于图像,因此我尝试了检测滑动动作并在检测到画面后更换屏幕。

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.carousel import Carousel
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup


class HomeScreen(Screen):
    def on_touch_move(self, touch):
        if touch.x < touch.ox: # this line checks if a left swipe has been detected
            MainApp().change_screen(screen_name="swipedhikr_screen") # calls the method in the main app that changes the screen


class ImageButton(ButtonBehavior, Image):
    pass


class LabelButton(ButtonBehavior, Label):
    pass


class SettingsScreen(Screen):
    pass


class SwipeDhikrScreen(Screen):
    pass


#def quit_verification():

 #   pop = Popup(title="verification", content=Label(text= "Are you sure?"))


GUI = Builder.load_file("main.kv")


class MainApp(App):
    def build(self):
        return GUI

    def change_screen(self, screen_name):
        # get the screen manager from the kv file
        screen_manager = self.root.ids["screen_manager"]
        screen_manager.transition.direction = "up"
        screen_manager.current = screen_name

    def quit_app(self):
        MainApp().stop()




MainApp().run()

我遇到了一个属性错误:“没有类型对象没有属性'ids'”

1 个答案:

答案 0 :(得分:2)

MainApp().change_screen(screen_name="swipedhikr_screen")

此行创建MainApp new 实例,该实例没有任何小部件,因此当您尝试访问它们时自然会失败。

使用MainApp的现有实例,即您正在通过MainApp.get_running_app()运行的实例。

同样,您对Carousel仅适用于图像不正确。