python - 如何在kivy python中通过滑动来更改屏幕

标签 python android kivy

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

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’”

最佳答案

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

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

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

此外,您认为轮播仅适用于图像也是不正确的。

关于python - 如何在kivy python中通过滑动来更改屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62488437/

相关文章:

python - 如何创建 Kivy 文件 (.kv)

python - Django-filter 'icontains' 没有传递到我的 URL

android - 使 android 按钮的背景半透明,

python - 如何确定字体是符号字体还是文本字体

java - String Builder 或 ""+someNumb 对于小案例是否更有效/更正确?

android - 编译 android 源代码时 linux-x86/bin/acp 的权限被拒绝

python - 如何使用Kivy在python中的TabbedPanel中排列Label、Entry

python - 有条件地改变特定单元格的背景颜色

python - 在 Pyramid 中使用 NumPy

python - 如何使用 python 获取 Excel 工作表的 "Total Editing Time"属性?