我有一个Kivy旋转木马,应该看起来像一本婴儿书。每张幻灯片上都有一个浮点数,每个浮点数都有一张图片、一个标签和一些文本。有几个问题,文本溢出到相邻的幻灯片上,我无法让它包装和/或调整自身大小以保持在幻灯片上的浮动中
我已经阅读了很多文档,尝试了很多关于pos_hint和size_hint以及text_size和size的东西,但是没有任何东西像我预期的那样有效。例如,见this和this和this和this
代码如下。它很长,但大部分只是向父窗口小部件添加窗口小部件
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()
有人能告诉我正确的方法吗。我想要的是文本在幻灯片上保持在浮动范围内,并通过使用窗口/幻灯片包装和/或调整大小/缩放来实现这一点
谢谢
我制作了两个小部件,它们继承了标准的Kivy小部件,并具有所需的行为。这可能不是最好的方法,但它是有效的
文本包装小部件 这个小部件就像一个“文本框”,里面的文本被包装起来。您可以用
Label
替换Button
,但按钮具有更多功能。Button
的所有属性和方法都可用文本大小调整小部件 这个小部件就像一个“文本框”,它里面的文本随着窗口的大小而调整大小。如您所见,底层小部件实际上是从
Button
中的纹理绘制的Image
。Button
的所有属性和方法都可用相关问题 更多 >
编程相关推荐