2017-10-06 68 views
0

我在製作一個圖片幻燈片,與python一起運行並用kivy執行時遇到問題。Kivy和Python創建一個幻燈片,當點擊時移動

我使用異步,但我想要使幻燈片顯示,以便我打開照片,然後當我點擊右邊前進,但如果鼠標點擊左側,然後它去往前頁面(圖片)。

感謝您的幫助。

回答

0

如果需要,您可以從按鈕中移除註釋#並移動左/右方法,但我認爲旋轉木馬方向功能可以解決該問題。請注意,我是一名新手,所以這只是我幫助的方式。我想我會幫助其他人,因爲我在這裏得到了很多幫助。由於

from kivy.app import App 
from kivy.loader import Loader 
from kivy.lang import Builder 
from kivy.base import runTouchApp 
from kivy.clock import Clock 
from kivy.properties import * 
from kivy.uix.image import AsyncImage 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.carousel import Carousel 
from kivy.uix.button import Button 

Builder.load_string(''' 
<MyWidget>: 
    carousel: carousel 
    cols: 1 
    Button: 
     pos_hint: {"center_x":0.5, "center_y":0.1} 
     size_hint: .3,.1 
     font_size: 35 
     text: str(root.on_off) 
     on_release: root.start_slide() 
    Carousel: 
     pos_hint: {"center_x":0.5, "center_y":0.9} 
     id: carousel 
     direction: 'right' 
     loop: True 
     index: 0 
''') 

class MyWidget(GridLayout): 
    on_off = StringProperty('Start') 
    slide_count = NumericProperty(11) 
    def __init__(self, **kwargs): 
     super(MyWidget, self).__init__() 
     self.carousel = Carousel(direction='right') 
     self.add_widget(self.carousel) 
     for i in range(self.slide_count): 
      src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i 
      image = AsyncImage(source=src, allow_stretch=True) 
      self.carousel.add_widget(image) 
     #self.start_slide()### Uncomment this to start slideshow when the app starts 

    def start_slide(self, *args): 
     if self.on_off == 'Start': 
      self.on_off = 'Stop' 
      self.clock = Clock.schedule_interval(self.slide_next, 3) ##move right every 3 seconds 
      return 
     if self.on_off == 'Stop': 
      self.on_off = 'Start' 
      Clock.unschedule(self.clock) 
      self.carousel.index = 0 

    def slide_next(self, *args): 
     if self.carousel.index == (self.slide_count - 1): 
      self.carousel.index = 0### This keeps the loops intact 
      #### if you want to end the slideshow at the last image, use 'Clock.unschedule(self.clock)' instead 
      return 
     self.carousel.load_next() 

class SlideShowApp(App): 

    def build(self): 
     mywidget = MyWidget() 
     return mywidget 

if __name__ == '__main__': 

    SlideShowApp().run() 

希望這是你需要什麼