class CarouselApp(App): def myChange(self, instance, value): print(self.carousel.index) pass def build(self): global page self.count = 0 self.carousel = Carousel(direction='right') self.carousel.bind(on_touch_move=self.myChange) self.carousel.scroll_distance = 1 self.carousel.scroll_timeout = 999 #self.carousel. box = BoxLayout(orientation='vertical', padding=3) self.text = Label(text=page[self.count], pos=(10, 0), width=300, height=100) for i in range(10): source = f'{i}.jpg' self.image = Image(source=source) self.carousel.add_widget(self.image) self.slide = self.carousel.next_slide box.add_widget(self.carousel) box.add_widget(self.text) return box
class Columns(GridLayout): def __init__(self, text, **kwargs): super(Columns, self).__init__(size_hint_x=1, rows=2) self.current_block = 0 self.carousel = Carousel(direction='right', size_hint_x=1, size_hint_y=1) self.carousel.bind(index=self.update_title) self.title_label = Label(font_name=FONT, markup=True, font_size=30, size_hint_y=0.1) self.update_content(text) def update_title(self, *kwargs): if len(kwargs) > 0: self.current_block = kwargs[1] or 0 title = emphasize_part(self.word.string, idx=self.current_block) etymology_str = (' ({})'.format( emphasize_part(self.word.etymology, idx=self.current_block)) if self.word.etymology else '') title += etymology_str self.title_label.text = title def analyze(self, text): self.word = Word(text, compute_etymology=True) self.blocks = self.word.get_blocks_for_selected_meaning() def update_content(self, text): self.clear_widgets() self.carousel.clear_widgets() self.analyze(text) self.update_title() for block in self.blocks: words = DbUtil().get_words_with_block(block, exclude=self.word) self.carousel.add_widget(BlockColumn(block, words)) self.add_widget(self.title_label) self.add_widget(self.carousel)
def on_activated(self,*args): super(VirtualScreenManagerStack, self).on_activated(*args) print 'on activated', self.activated, self.current_screen SCALE = .6 if self.activated else 1 if self.activated: self.clear_widgets() grid = Carousel(size_hint = (1,1), width=.8*Window.width, x=.1*Window.width, direction="bottom") FloatLayout.add_widget(self,grid) for wrapper in self.wrappers.values(): print 'before', wrapper.size wrapper.scale=SCALE print 'after', wrapper.size if self.activated: wrapper.size_hint= None, None print 'adding wrapper of ', wrapper.height grid.add_widget(wrapper) else: self.children[0].remove_widget(wrapper) if not self.activated: self.clear_widgets() w= self.wrappers[self.current_screen] w.pos = 0,0 w.size_hint= 1,1 FloatLayout.add_widget(self, w) else: print "grid", grid.size, grid.pos, grid.size_hint return if 0: self.clear_widgets() sv = ScrollView() with sv.canvas.before: Color(.3,.3,.3) Rectangle(size=self.size, pos=self.pos) FloatLayout.add_widget(self,sv) grid= GridLayout() sv.do_scroll_x = self.activated sv.do_scroll_y = self.activated grid.rows = 1 if self.orientation=='horizontal' else None grid.cols = 1 if self.orientation=="vertical" else None grid.bind(minimum_height = grid.setter('height')) grid.bind(minimum_width = grid.setter('width')) grid.spacing = self.spacing sv.add_widget(grid) SCALE = .6 # if self.orientation == 'vertical': # attrName = "cols" # dh_name = "row_default_height" # default_value = SCALE*self.height #300 # size_hint=(1,None) # else: # attrName = "rows" # dh_name = "col_default_width" # default_value = 400 # size_hint=(None,1) # kwargs = {attrName:2, dh_name:default_value} # kwargs = {attrName:2} # kwargs = {attrName:1} # kwargs = {attrName:1, dh_name:default_value} # if self.orientation == 'vertical': # grid.height = len(self.content)*(SCALE*self.height+self.spacing) # else: # grid.width = len(self.content)*(SCALE*self.width + self.spacing) for sindex, screen in enumerate(self.content): wrapper = self.wrappers[screen] wrapper.size_hint = None,None wrapper.size = self.size if 1 or screen != self.current_screen: wrapper.scale = SCALE else: reducer = Animation(scale=SCALE, opacity=.5, duration=.3) reducer.start(wrapper) grid.add_widget(wrapper) #Clock.schedule_once(adder,.31) #if self.orientation=='vertical': # name = wrapper.screen_name or str(screen) # grid.add_widget(MagicLabel(target= wrapper, text=name, size = wrapper.size, texture_size = (800,600), size_hint_x=None, width=60)) ##if self.orientation =="horizontal": ## for screen in self.content: ## name = wrapper.screen_name or str(screen) ## grid.add_widget(MagicLabel(target= wrapper, text=name, size = wrapper.size, texture_size = (800,600), size_hint_y=None, height=30)) #scrool sv to proper height ##if self.orientation=="vertical": ## sv.scroll_y= 1- float(self.content.index(self.current_screen))/len(self.content) ##else: ## sv.scroll_x= float(self.content.index(self.current_screen))/len(self.content) else: sv = self.children[0] grid = sv.children[0] for screen in self.content: wrapper = self.wrappers[screen] grid.remove_widget(wrapper) self.clear_widgets() w = self.wrappers[self.current_screen] w.size_hint = 1,1 FloatLayout.add_widget(self,w) wrapper = self.wrappers[self.current_screen] expander = Animation(scale = 1, opacity=1, x=0, y=0, duration=.3) expander.start(wrapper)
lb3 = Label(text='Slide_3') root.add_widget(lb1) root.add_widget(lb2) root.add_widget(lb3) root.loop = True # Auto swipe # callback function def auto_swipe(dt): print(dt) root.load_next() return True # schedule tm = Clock.schedule_interval(auto_swipe, 3) print(type(tm)) # callback function to cancel def cancel_swipe(self, t): print('auto swipe canceled') Clock.unschedule(tm) root.bind(on_touch_up=cancel_swipe) Window.size = (300, 150) ap = kivy05() ap.run()
class CarouselApp(App): def myChange(self, instance, value): print(self.carousel.index + 1) pass def build(self): global page self.window = FloatLayout() self.carousel = Carousel(direction='right') self.carousel.bind(on_touch_move=self.myChange) self.carousel.scroll_distance = 1 self.carousel.scroll_timeout = 999 self.layout = [] # carousel for i in range(10): self.layout.append( FloatLayout(size_hint=(1, .55), pos_hint={ 'x': 0, 'y': .4 })) source = f'{i}.jpg' self.image = Image(source=source, pos_hint={ 'center_x': .5, 'center_y': .4 }) self.myText = Label(text=page[i], halign='center', valign='top', text_size=[_WIDTH, 100], pos_hint={ 'x': 0, 'y': -.8 }, color='#4B0082') self.layout[i].add_widget(self.image) self.layout[i].add_widget(self.myText) self.carousel.add_widget(self.layout[i]) # window self.btn_next = Button(text='Далее', size_hint=(.455, .08), pos_hint={ 'center_x': .745, 'center_y': .1 }, on_release=self.evt_btn_next) self.btn_pre = Button(text='Назад', size_hint=(.455, .08), pos_hint={ 'center_x': .255, 'center_y': .1 }, on_release=self.evt_btn_pre) self.window.add_widget(self.carousel) self.window.add_widget(self.btn_pre) self.window.add_widget(self.btn_next) return self.window def evt_btn_next(self, instance): self.carousel.load_next(mode='next') def evt_btn_pre(self, instance): self.carousel.load_previous()
class KivyVisor(ModalView): MODO_NORMAL = 1 MODO_AJUSTADO_ALTURA = 2 MODO_AJUSTADO_ANCHO = 3 def __init__(self, comicBook, **kwargs): super(KivyVisor, self).__init__(**kwargs) self.modoVisualizacion = KivyVisor.MODO_NORMAL self.carrusel = Carousel() self.bind(on_touch_down=self.on_touch) self.comic = comicBook self.comic.openCbFile() self.matrizTransfPagina = None self.listaPaginas = [] self.__loadComicBooks__() self.carrusel.bind(index=self.carruselSlide) self.add_widget(self.carrusel) def __updateCarrusell__(self, args): imagenPagina = self.comic.getImagePage() imagenPagina.size = imagenPagina.texture_size imagenPagina.size_hint = (None, None) imagenPagina.size_hint = (None, None) imagenPagina.keep_ratio = True scrollView = ScrollView() scrollView.add_widget(imagenPagina) self.carrusel.add_widget(scrollView) self.listaPaginas.append(imagenPagina) if not self.comic.gotoNextPage(): Clock.unschedule(self.hiloCargaThumnails) return False def __loadComicBooks__(self): self.carrusel.clear_widgets() self.hiloCargaThumnails = Clock.schedule_interval( self.__updateCarrusell__, 1) def carruselSlide(self, args, args2): self.carrusel.current_slide.scroll_y = 1 self.carrusel.next_slide.scroll_y = 1 # if scatter.transform!=self.matrizTransfPagina: # scatter.apply_transform(self.matrizTransfPagina) def on_transform_with_touch(self, touch, args): print(touch.scale) for slide in self.carrusel.slides: if slide != touch: slide.scale = touch.scale slide.pos = touch.pos # slide.apply_transform(touch.transform) # self.matrizTransfPagina = touch.transform def on_sizeWindow(self, arg1, arg2, arg3): self.__refreshPage__() def on_motion(self, etype, motionevent, other): if other.is_mouse_scrolling: if other.button == 'scrolldown': self.scatter.y -= 10 if other.button == 'scrollup': self.scatter.y += 10 else: # print(self.scatter.pos) print("pos imagen :{}".format(self.imagenPagina.pos)) print("tamaño scatter :{}".format(self.scatter.size)) # help(other) # print(motionevent) # print(other) # print("Capturan scroll") def on_touch(self, obj, event): ''' vamos a capturar eventos en estas zonas ************************* *1* *0* *2* *** *** *** * * * * * * * * *3* *4* * * * * * * * * * * *** *** *5* *6* ************************* :param widget: :param event: :return: ''' #zona1 = ((0, Window.width * 0.1), (Window.height, (Window.height - Window.height * 0.1))) #zona2 = ((Window.width - Window.width * 0.1, Window.width), (Window.height, (Window.height - Window.height * 0.1))) zona0 = ((Window.width * 0.5 - Window.width * 0.1, Window.width * 0.5 + Window.width * 0.1), (Window.height - Window.height * 0.1, Window.height)) zona3 = ((0, Window.width * 0.1), (Window.height * 0.1 + Window.height * 0.5, Window.height * -0.1 + Window.height * 0.5)) zona4 = ((Window.width - Window.width * 0.1, Window.width), (Window.height * 0.1 + Window.height * 0.5, Window.height * -0.1 + Window.height * 0.5)) if (zona0[0][0] < event.pos[0] and event.pos[0] < zona0[0][1]) and ( zona0[1][0] < event.pos[1] and event.pos[1] < zona0[1][1]): box = GridLayout(cols=5) botonAncho = Button(text="Ancho") botonAncho.bind(on_press=self.ancho) box.add_widget(botonAncho) botonAjustarAlto = Button(text="Alto") botonAjustarAlto.bind(on_press=self.ajustarAlto) box.add_widget(botonAjustarAlto) botonCentrado = Button(text="normal") botonCentrado.bind(on_press=self.sinAjuste) box.add_widget(botonCentrado) botonRotar = Button(text="Rotar") botonRotar.bind(on_press=self.rotar) box.add_widget(botonRotar) p = Popup(title='Comic View popup', size_hint=(None, None), size=(400, 150)) p.add_widget(box) p.open() def __refreshPage__(self): pass def ancho(self, event): self.modoVisualizacion = KivyVisor.MODO_AJUSTADO_ANCHO for imagen in self.listaPaginas: if imagen.image_ratio < 1: imagen.width = Window.width imagen.height = Window.width / (imagen.image_ratio) else: imagen.width = Window.width imagen.height = Window.width * (imagen.image_ratio) for scroll in self.carrusel.slides: self.carrusel.current_slide.scroll_y = 0.5 def ajustarAlto(self, event): self.modoVisualizacion = KivyVisor.MODO_AJUSTADO_ALTURA for imagen in self.listaPaginas: if imagen.image_ratio < 1: imagen.width = Window.height / (imagen.image_ratio) imagen.height = Window.height else: imagen.width = Window.height * (imagen.image_ratio) imagen.height = Window.height for scroll in self.carrusel.slides: self.carrusel.current_slide.scroll_x = 0 def rotar(self, event): print(self.carrusel.current_slide.size) print(self.listaPaginas[self.carrusel.index].pos) self.listaPaginas[self.carrusel.index].pos[0] += 1 def sinAjuste(self, event): self.listaPaginas[self.carrusel.index].height = self.listaPaginas[ self.carrusel.index].originalHeight self.listaPaginas[self.carrusel.index].width = self.listaPaginas[ self.carrusel.index].originalWidth