我正在尝试通过滑动屏幕切换到另一个屏幕。我尝试过轮播,但似乎它只适用于图像,因此我尝试检测滑动 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/