在KivyMD (Python)中自动扫描/滚动轮播



我想在KivyMDPython中制作一个自动滚动的2滑动旋转木马。在启动时,应用程序从旋转木马的第一张幻灯片开始,并应在3秒后更改为第二张幻灯片。

这是我的代码.kv

<WelcomeScreen>:
MDFloatLayout:
md_bg_color : 1, 1, 1, 1
Carousel:
id: caraousel
on_current_slide: app.current_slide(self.index)
MDFloatLayout:
Image:
source: "Assets/1.png"
pos_hint: {"center_x": .5, "center_y": .6}
size_hint: .3, .3
MDLabel:
text: "Slide 1"
pos_hint: {"center_y": .087}
halign: "center"
font_name: "Poppins-Light"
font_size: "14sp"
color: rgba(135, 143, 158, 200)
MDFloatLayout:
Image:
source: "Assets/2.jpg"
pos_hint: {"center_x": .5, "center_y": .7}
size_hint: .8, .8
MDLabel:
text: "Slide 2"
pos_hint: {"center_y": .47}
halign: "center"
font_name: "Poppins-Regular"
font_size: "25px"
color: rgba(1, 3, 23, 225)

. py

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen,ScreenManager, NoTransition
from kivy.utils import rgba
from kivy.core.window import Window
from kivy.core.text import LabelBase
from kivy.clock import Clock
Window.size = (310, 580)
class WelcomeScreen(Screen):
pass
class AppApp (MDApp):
def build(self):
return Builder.load_file('app.kv')
def current_slide(self, index):
pass
AppApp().run()
谁能帮我解决这个问题?提前感谢。

可以使用Clock.schedule_interval方法来自动加载滑块。从代码中的任何地方触发该动作,例如,为了使它从一开始就发生,从类app的on_start方法触发它,

app.kv文件。

<WelcomeScreen>:
MDFloatLayout:
md_bg_color : 1, 1, 1, 1
Carousel:
id: caraousel
on_current_slide: app.current_slide(self.index)
MDFloatLayout:
Image:
source: "Assets/1.png"
pos_hint: {"center_x": .5, "center_y": .6}
size_hint: .3, .3
MDLabel:
text: "Slide 1"
pos_hint: {"center_y": .087}
halign: "center"
#                font_name: "Poppins-Light"
font_size: "14sp"
color: rgba(135, 143, 158, 200)
MDFloatLayout:
Image:
source: "Assets/2.jpg"
pos_hint: {"center_x": .5, "center_y": .7}
size_hint: .8, .8
MDLabel:
text: "Slide 2"
pos_hint: {"center_y": .47}
halign: "center"
#                font_name: "Poppins-Regular"
font_size: "25px"
color: rgba(1, 3, 23, 225)

main.py文件。

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen,ScreenManager, NoTransition
from kivy.utils import rgba
from kivy.core.window import Window
from kivy.core.text import LabelBase
from kivy.clock import Clock
Window.size = (310, 580)
class WelcomeScreen(Screen):
pass
class AppApp(MDApp):
def build(self):
Builder.load_file('app.kv')
return WelcomeScreen()
def on_start(self):
# Access the carousel.
carousel = self.root.ids.caraousel
# Set infinite looping (optional).
carousel.loop = True
# Schedule after every 3 seconds.
Clock.schedule_interval(carousel.load_next, 3.0)

def current_slide(self, index):
pass
AppApp().run()

更新:

尽量避免应用程序的实例名称为AppApp,相关的。kv文件同时为app.kv,这可能会导致自动加载。kv时出现问题。然而,显式加载文件,然后从build方法返回根(或在.kv文件中声明)应该工作。

最新更新