我正在尝试创建一个按钮,以更改/返回轮播中的上一个屏幕。
我习惯于管理.kv文件中的按钮,但对于轮播来说是不可能的,如果我这样做的话,最后一张幻灯片上只会创建一个按钮。
我需要在每张幻灯片上都显示该按钮,所以我做到了这一点,而且效果很好,除了我不知道如何将按钮链接到ScreenManager。
我的python文件的一部分:
class MyCarousel(Carousel):
def __init__(self, **kwargs):
super(MyCarousel, self).__init__(**kwargs)
self.direction = "right"
log_file = open_logfile()
for i in range(log_file['len']):
src = log_file['path'][(log_file['len']) - (i + 1)] + "_file_resize.jpg"
button = Button(size_hint=(.15,.25),\
pos_hint={'center_x': .05, 'y': .65},\
background_color=(1, 1, 1, 1),\
border=(0, 0, 0, 0),\
on_release=***???***
background_normal="Interface_PNG/bouton/Bouton_retour.png",\
background_down="Interface_PNG/bouton/Bouton_retour_fonce.png")
image = AsyncImage(source=src, allow_stretch=True, size_hint=(1, 1), pos_hint={'center_x': .5, 'y': 0})
layout = FloatLayout()
layout.add_widget(image)
layout.add_widget(button)
self.add_widget(layout)
self.loop = True
class StartScreen(Screen):
pass
class GalleryPhoto(Screen):
def call_next(self, dt):
self.manager.current = "start"
class RootScreen(ScreenManager):
pass
class PhotoboothApp(App):
def build(self):
sm = RootScreen()
self.start_screen = StartScreen()
self.gallery_photo = GalleryPhoto()
sm.add_widget(self.start_screen)
sm.add_widget(self.gallery_photo)
return sm
我的kv文件的一部分:
<StartScreen>:
name: "start"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
FloatLayout:
Image:
source: "Interface_PNG/Page_1.png"
allow_stretch : True
keep_ratio : False
size_hint: 1, 1
Button:
background_color: 0, 0, 0, 0
on_release: root.manager.current = "gallery"
<GalleryPhoto>:
name: "gallery"
FloatLayout:
Image:
source: "Interface_PNG/Page_8.png"
allow_stretch: True
keep_ratio: False
size_hint: 1, 1
MyCarousel:
我想在python中做类似的事情:
MyCarousel:
Button:
on_release: root.manager.current = "start"
我认为这很简单,但是我有点迷路了...欢迎任何帮助。
答案 0 :(得分:1)
change_screen()
中实现class MyCarousel()
方法change_screen()
方法App.get_running_app().root
访问实例化的根ScreenManager
对象。class MyCarousel(Carousel):
def __init__(self, **kwargs):
...
button = Button(size_hint=(.15,.25), pos_hint={'center_x': .05, 'y': .65},
background_color=(1, 1, 1, 1), border=(0, 0, 0, 0),
on_release=self.change_screen(),
...
def change_screen(self, instance):
App.get_running_app().root.current = 'start'
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
class MyCarousel(Carousel):
def __init__(self, **kwargs):
super(MyCarousel, self).__init__(**kwargs)
self.direction = "right"
for i in range(10):
src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i
image = AsyncImage(source=src, allow_stretch=True, size_hint=(1, 1), pos_hint={'center_x': .5, 'y': 0})
button = Button(size_hint=(.15, .25), pos_hint={'center_x': .05, 'y': .65},
text='StartScreen',
on_release=self.change_screen)
layout = FloatLayout()
layout.add_widget(image)
layout.add_widget(button)
self.add_widget(layout)
self.loop = True
def change_screen(self, instance):
App.get_running_app().root.current = 'start'
class StartScreen(Screen):
pass
class GalleryPhoto(Screen):
pass
class RootScreen(ScreenManager):
pass
Builder.load_file("main.kv")
class TestApp(App):
title = "Photobooth"
def build(self):
return RootScreen()
if __name__ == "__main__":
TestApp().run()
<RootScreen>:
StartScreen:
GalleryPhoto:
<StartScreen>:
name: "start"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
FloatLayout:
Image:
source: "kivy-logo.png"
allow_stretch : True
keep_ratio : True # False
size_hint: 1, 1
Button:
background_color: 0, 0, 0, 0
on_release: root.manager.current = "gallery"
<GalleryPhoto>:
name: "gallery"
FloatLayout:
Image:
source: "kivymd_logo.png"
allow_stretch: True
keep_ratio: False
size_hint: 1, 1
MyCarousel: