admin管理员组文章数量:1406943
I have this code that populates my Carousel with images from the path:
class EzLaunchScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
img_path = ('D:\\Steven\Desktop\\Python App\\Images\\Carousel\\')
# Funtion get images int carousel...
layout = FloatLayout()
self.background_image = Image(source='Images\launchScreen.png')
carousel_widget = Carousel(direction ='right')
for img_file in os.listdir(img_path):
if img_file.endswith(('.png', '.jpg', '.jpeg')):
img = Image(source=os.path.join(img_path, img_file))
print(f"Adding image: {img_file}")
carousel_widget.add_widget(img)
layout.add_widget(self.background_image)
layout.add_widget(carousel_widget)
self.add_widget(layout)
And in string snippet I have the following
Code = '''
<EzLaunchScreen>:
brand_text: Brand_label
val_text: ToT_label
slide_text: slider_label
name: 'EzLaunch'
BoxLayout:
Button:
background_color: 0, 0, 0, 0
size_hint: .25, .070
on_press: app.swipe_me()
pos_hint: {'center_x': 0.10, 'center_y': .975}
# This is the carousel library of brands...
Carousel:
id: carousel_widget
direction: 'right'
on_slide: root.swipe_me()
'''
I want a function that gives me the image name when I swipe the carousel... This is what I've tried but does not work:
def swipe_me(self, carousel_widget_instance, value):
current_image = carousel_widget_instance.slides[carousel_widget_instance.index]
image_name = current_image.image_name # Access the stored name
print(f"Current image name: {image_name}")
Cross posted HERE
I have this code that populates my Carousel with images from the path:
class EzLaunchScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
img_path = ('D:\\Steven\Desktop\\Python App\\Images\\Carousel\\')
# Funtion get images int carousel...
layout = FloatLayout()
self.background_image = Image(source='Images\launchScreen.png')
carousel_widget = Carousel(direction ='right')
for img_file in os.listdir(img_path):
if img_file.endswith(('.png', '.jpg', '.jpeg')):
img = Image(source=os.path.join(img_path, img_file))
print(f"Adding image: {img_file}")
carousel_widget.add_widget(img)
layout.add_widget(self.background_image)
layout.add_widget(carousel_widget)
self.add_widget(layout)
And in string snippet I have the following
Code = '''
<EzLaunchScreen>:
brand_text: Brand_label
val_text: ToT_label
slide_text: slider_label
name: 'EzLaunch'
BoxLayout:
Button:
background_color: 0, 0, 0, 0
size_hint: .25, .070
on_press: app.swipe_me()
pos_hint: {'center_x': 0.10, 'center_y': .975}
# This is the carousel library of brands...
Carousel:
id: carousel_widget
direction: 'right'
on_slide: root.swipe_me()
'''
I want a function that gives me the image name when I swipe the carousel... This is what I've tried but does not work:
def swipe_me(self, carousel_widget_instance, value):
current_image = carousel_widget_instance.slides[carousel_widget_instance.index]
image_name = current_image.image_name # Access the stored name
print(f"Current image name: {image_name}")
Cross posted HERE
Share Improve this question edited Mar 11 at 18:47 Starship Remembers Shadow 9944 gold badges15 silver badges28 bronze badges asked Mar 7 at 6:25 Steven GerberSteven Gerber 113 bronze badges 2- this solves... carousel_widget.bind(_index=self.swipe_me) def swipe_me(self, instance, value): current_image = instance.slides[int(value)] image_name = current_image.source.split('\\')[-1] image_name = image_name.split('.')[0] self.brand_text.text = image_name.upper() print(f"Image Name: {image_name.upper()}") – Steven Gerber Commented Mar 7 at 9:49
- 1 Welcome to SO. Please post the solution as an answer below, not as an edit to the question. – desertnaut Commented Mar 10 at 14:46
1 Answer
Reset to default 0According to the OP, this solved the problem:
carousel_widget.bind(_index=self.swipe_me)
def swipe_me(self, instance, value):
current_image = instance.slides[int(value)]
image_name = current_image.source.split('\\')[-1]
image_name = image_name.split('.')[0]
self.brand_text.text = image_name.upper()
print(f"Image Name: {image_name.upper()}")
本文标签: pythonExternal button code to get carousel image attributeStack Overflow
版权声明:本文标题:python - External button code to get carousel image attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744945193a2633731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论