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

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

问题描述

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

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 \n 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()

谢谢.

推荐答案

因此,在给我的问题多了思考并尝试了不同的方法后,我最终得到了一个合适的"just 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 Tutorials官方建议,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教程书建议的设置.需要有一个函数来更新Label位置及其文本大小(此处为setting_function)以与Label大小绑定.另外,我将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来解决此问题的原因(而不仅仅是使用处理问题的奇异语言)是因为我想拥有在运行时动态更改的变量,用作标签参数.而且我只是因为我很固执而不得不找出答案.

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天全站免登陆