包装 Kivy 标签的文本 [英] Wrapping the text of a Kivy Label

查看:16
本文介绍了包装 Kivy 标签的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用 Kivy GUI 编写程序,我真的不想使用 .kv 文件,而是将其全部写入 python 文件.问题是在 MainScreen 类中,我想添加一些标签,但我无法根据窗口大小的变化使它们的文本换行.当我尝试 self.size 时,我只得到 100x100.我已经尝试了 Kivy 教程书中的所有建议,但没有任何效果.我知道我可以使用 使文本多行或设置标准标签大小,但这不是一个真正的解决方案.我需要标签大小和文本来跟随整个窗口的变化和要换行的文本.

So I am writting a program using the Kivy GUI and I really don't want to use a .kv file but write it all in the python file. The problem is that inside the MainScreen Class i want to add some Labels but i cannot make their text to wrap according to window size changes. When i try self.size I only get 100x100. I have tried all the suggestions from the Kivy tutorial book but nothing worked. I know i could just make the text multiline using or set a standard Label size, but that wouldn't be a real solution. I need the Label size and text to follow the whole window changes and the text to be wrapped.

我已经简化了程序,只关注这个问题.

I have simplified the program to focus just on this issue.

代码如下:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.graphics import Rectangle, Color


class MainScreen(FloatLayout):
    """MAIN WINDOW CLASS"""

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

        with self.canvas.before:
            Color(0.988, 0.725, 0.074, 1, mode='rgba')
            self.rect = Rectangle(pos=self.pos, size=self.size)
        self.bind(size=self.update_rect)


        #TITLE LABEL
        self.add_widget(Label(text="A very Long Sentence that needs to be wrapped.",
                              bold = True,
                              font_size="20sp",
                              pos_hint={'center_x': 0.5, 'center_y': .85},
                              size_hint=(None, None),
                              halign="center",
                              color=(0.055, 0.235, 0.541, 1)))

    def update_rect(self, *args):
        """FUNCTION TO UPDATE THE RECATANGLE OF CANVAS TO FIT THE WHOLE SCREEN OF MAINSCREEN ALWAYS"""
        self.rect.pos = self.pos
        self.rect.size = self.size

class main(App):
    """BUILDING THE APP"""
    def build(self):
        return MainScreen()

if __name__ == "__main__":
    main().run()

谢谢.

推荐答案

所以在对我的问题进行了深思熟虑并尝试了不同的方法之后,我最终得到了一个合适的仅 python"解决方案.

So after giving my problem a lot of thought and experimenting with different approaches i ended up with a proper "just python" solution.

首先我的回答是基于 1) 官方 Kivy 教程建议,2) 画布方法的常用操作和 3) 对变量范围的谨慎处理.

First of all my answer is based on 1) the official Kivy Tutorials suggestions, 2) the usual manipulation of canvas approach and 3) careful handling on variable scope.

因此标签设置类似于 Kivy 教程书中建议的设置.需要有一个函数来更新标签位置及其文本大小(此处为 setting_function)以与标签大小绑定.此外,我将 Label 分配给一个变量,以便以后可以轻松引用它.

So the Label settings are similar to the ones suggested by the Kivy tutorial book. There needs to be a function that updates the Label position and it's text size (the setting_function here) to bind with the Label size. Also I assigned the Label to a variable so I could easily refer to it later.

在所有这些更改之后,我的代码如下所示:

After all those changes my code looks like this:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.graphics import Rectangle, Color


class MainScreen(FloatLayout, Label):
    """MAIN WINDOW CLASS"""

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

        with self.canvas.before:
            Color(0.988, 0.725, 0.074, 1, mode='rgba')
            self.rect = Rectangle(pos=self.pos, size=self.size)
        self.bind(size=self.update_rect)

        #TITLE LABEL
        self.titlos = Label(text="A very Long Sentence that needs to be wrapped.",
                              bold = True,
                              text_size=(None,None),
                              font_size="20sp",
                              pos_hint={'center_x': 0.5, 'center_y': .85},
                              size_hint_y=None,
                              size = self.size,
                              height=self.texture_size[1],
                              halign="center",
                              valign = "middle",
                              color=(0.055, 0.235, 0.541, 1))

        self.add_widget(self.titlos)
        self.titlos.bind(size=self.setting_function)

    def setting_function(self, *args):
        """FUNCTION TO UPDATE THE LABEL TO ADJUST ITSELF ACCORDING TO SCREEN SIZE CHANGES"""
        self.titlos.pos_hint = {'center_x': 0.5, 'center_y': .85}
        self.titlos.text_size=self.size

    def update_rect(self, *args):
        """FUNCTION TO UPDATE THE RECATANGLE OF CANVAS TO FIT THE WHOLE SCREEN OF MAINSCREEN ALWAYS"""
        self.rect.pos = self.pos
        self.rect.size = self.size


class main(App):
    """BUILDING THE APP"""
    def build(self):
        return MainScreen()

if __name__ == "__main__":
    main().run()

上面的代码确实符合我在问题中设置的要求.很容易将它用于您自己的项目.只需注意变量的范围并使用大量打印消息进行检查.

The above code does meet the requirements I set in my question. It is easy to use it to your own projects. Just take care of the variables' scopes and use lots of print messages for checking.

最后我想只使用 Python 来解决这个问题(而不仅仅是使用 kivy 语言来处理这些问题就像小菜一碟)的原因是我想要在运行时动态更改变量,即用作标签参数.而且我只需要找出答案,因为我很固执.

Finally the reason I wanted to use just Python to solve this problem (and not just use kivy language which handles those issues like a piece of cake) is that I want to have variables that are dynamically changed during the run time, that are used as the Label parameters. And also I just had to find out because I am stubborn.

谢谢.

这篇关于包装 Kivy 标签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆