gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:37:39 27 4
gpt4 key购买 nike

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

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 的现有实例,即您实际运行的那个,通过 MainApp.get_running_app() .
此外,您认为 Carousel 仅适用于图像也是不正确的。

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com