如何在新类中更改方法的内容



我正在尝试通过滑动来更改屏幕更改的过渡方向,但我无法实现。屏幕正在更改,但我希望更改的方向不同。我该怎么做?

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:
MainApp.get_running_app().change_screen(screen_name="swipedhikr_screen")
#MainApp.get_running_app().screen_manager.transition.direction = "left" ----> also didn't work
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"  # I also tried screen_direction = screen_manager.transition.direction
# screen_direction = "up"
screen_manager.current = screen_name
def quit_app(self):
MainApp().stop()

MainApp().run()

我尝试将screen_manager.transition.direction设置为变量,然后将该变量设置为"up",以便我可以访问该变量并在class HomeScreen(Screen)中对其进行操作,但这不起作用。

不确定,但我认为您可以使用switch_to()

def change_screen(self, screen_name):
screen_manager.switch_to(screen_name, direction='up')

请参阅文档:https://kivy.org/doc/stable/api-kivy.uix.screenmanager.html#kivy.uix.screenmanager.ScreenManager.switch_to

当我将方向作为参数传递时,它有效,因此它变成了

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

最新更新