将文本放置在Kivy上,并将其包裹在旋转木马中的幻灯片上,而不会溢出到相邻的幻灯片中

2024-10-02 02:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Kivy旋转木马,应该看起来像一本婴儿书。每张幻灯片上都有一个浮点数,每个浮点数都有一张图片、一个标签和一些文本。有几个问题,文本溢出到相邻的幻灯片上,我无法让它包装和/或调整自身大小以保持在幻灯片上的浮动中

我已经阅读了很多文档,尝试了很多关于pos_hint和size_hint以及text_size和size的东西,但是没有任何东西像我预期的那样有效。例如,见thisthisthisthis

Wide view showing all text

Narrow view showing no wrapping, no resizing, and text spilling

代码如下。它很长,但大部分只是向父窗口小部件添加窗口小部件

import kivy 
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.label import Label
from kivy.uix.image import AsyncImage 
from kivy.uix.carousel import Carousel 

def render_kivy_carousel(): 
    # Set the windows background colour.
    Window.clearcolor = (1, 1, 0, 1)
    # Create the book renditions root widget.
    bkr = Carousel() 
    bkr.width = 100
    bkr.loop = True
    # Add the page widgets.

    """PAGE 1
    """
    # Create the pages base widget
    flo = FloatLayout()

    # Build the image
    image = AsyncImage(source='https://wallpaperaccess.com/full/195082.jpg',
                allow_stretch = True,
                keep_ratio = True,
                size_hint = (0.5, 0.5),
                pos_hint = {'x': 0.25, 'y': 0.25},)
    flo.add_widget(image)

    # Build the label
    label = Label(text='Bee',
                color=(0,0,0,1),
                font_size=60,
                size_hint=(0.25, 0.25),
                pos_hint={'bottom': 1, 'left': 1},)
    flo.add_widget(label)
    
    # Build the text
    text = Label(text='The bee buzzes looking for flowers',
                color=(0,0,0,1),
                font_size=20,
                size_hint=(0.25, 0.25),
                pos_hint={'top': 1, 'right': 1},)
    flo.add_widget(text)
    bkr.add_widget(flo)

    """PAGE 2
    """
    # Create the pages base widget
    flo = FloatLayout()

    # Build the image
    image = AsyncImage(source='https://i.pinimg.com/originals/60/5b/c0/605bc0c0ef2645b43d64300bb2d82ed1.jpg',
                allow_stretch = True,
                keep_ratio = True,
                size_hint = (0.5, 0.5),
                pos_hint = {'x': 0.25, 'y': 0.25},)
    flo.add_widget(image)

    # Build the label
    label = Label(text='Beetle',
                color=(0,0,0,1),
                font_size=60,
                size_hint=(0.25, 0.25),
                pos_hint={'bottom': 1, 'left': 1},)
    flo.add_widget(label)
    
    # Build the text
    text = Label(text='The beetle looks at the sky',
                color=(0,0,0,1),
                font_size=20,
                size_hint=(0.25, 0.25),
                pos_hint={'top': 1, 'right': 1},)
    flo.add_widget(text)
    bkr.add_widget(flo)

    """PAGE 3
    """
    # Create the pages base widget
    flo = FloatLayout()

    # Build the image
    image = AsyncImage(source='https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQac3tmjWwO-o62Cm3iUAby6EVLWNG226UOJQ&usqp=CAU',
                allow_stretch = True,
                keep_ratio = True,
                size_hint = (0.5, 0.5),
                pos_hint = {'x': 0.25, 'y': 0.25},)
    flo.add_widget(image)

    # Build the label
    label = Label(text='Butterfly',
                color=(0,0,0,1),
                font_size=60,
                size_hint=(0.25, 0.25),
                pos_hint={'bottom': 1, 'left': 1},)
    flo.add_widget(label)
    
    # Build the text
    text = Label(text='The butterfly flaps its wings',
                color=(0,0,0,1),
                font_size=20,
                size_hint=(0.25, 0.25),
                pos_hint={'top': 1, 'right': 1},)
    flo.add_widget(text)
    bkr.add_widget(flo)

    # Return the root widget
    return bkr

# Create the App class 
class BookletApp(App): 
    def build(self): 
        return render_kivy_carousel()

# Run the App 
if __name__ == '__main__': 
    BookletApp().run()

有人能告诉我正确的方法吗。我想要的是文本在幻灯片上保持在浮动范围内,并通过使用窗口/幻灯片包装和/或调整大小/缩放来实现这一点

谢谢


Tags: thetextposimageimportbuildaddsize
1条回答
网友
1楼 · 发布于 2024-10-02 02:28:24

我制作了两个小部件,它们继承了标准的Kivy小部件,并具有所需的行为。这可能不是最好的方法,但它是有效的

文本包装小部件 这个小部件就像一个“文本框”,里面的文本被包装起来。您可以用Label替换Button,但按钮具有更多功能。Button的所有属性和方法都可用

from kivy.uix.button import Button
...
class WrapBox(Button):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.bind(
            width=lambda *x:
                self.setter('text_size')(self, (self.width, None)),
            texture_size=lambda *x: 
                self.setter('height')(self, self.texture_size[1]))
...
txtbox = WrapBox(text='wrap me up in your love baby')
txtbox.color = (0,0,0,1)
txtbox.font_size = 60

文本大小调整小部件 这个小部件就像一个“文本框”,它里面的文本随着窗口的大小而调整大小。如您所见,底层小部件实际上是从Button中的纹理绘制的ImageButton的所有属性和方法都可用

from kivy.uix.image import Image
from kivy.uix.button import Button
...
class ResizeBox(Image):
    text = StringProperty('')
    def on_text(self, *args):
        # Just get large texture:
        lbl = Button(text=self.text)
        # Value that'll give texture bigger than screen size
        # Small values result in blur.
        lbl.font_size = '1000dp'  
        lbl.texture_update()
        # Set it to image, it'll be scaled to image size automatically
        self.texture = lbl.texture
...
txtbox = ResizeBox(text='resize me with your love baby')
txtbox.color = (0,0,0,1)
txtbox.font_size = 60

Wrapped and Resized

相关问题 更多 >

    热门问题