• <tfoot id='4w7RQ'></tfoot>
    <legend id='4w7RQ'><style id='4w7RQ'><dir id='4w7RQ'><q id='4w7RQ'></q></dir></style></legend>
    1. <small id='4w7RQ'></small><noframes id='4w7RQ'>

    2. <i id='4w7RQ'><tr id='4w7RQ'><dt id='4w7RQ'><q id='4w7RQ'><span id='4w7RQ'><b id='4w7RQ'><form id='4w7RQ'><ins id='4w7RQ'></ins><ul id='4w7RQ'></ul><sub id='4w7RQ'></sub></form><legend id='4w7RQ'></legend><bdo id='4w7RQ'><pre id='4w7RQ'><center id='4w7RQ'></center></pre></bdo></b><th id='4w7RQ'></th></span></q></dt></tr></i><div id='4w7RQ'><tfoot id='4w7RQ'></tfoot><dl id='4w7RQ'><fieldset id='4w7RQ'></fieldset></dl></div>

        <bdo id='4w7RQ'></bdo><ul id='4w7RQ'></ul>

      1. 包装 Kivy 标签的文本

        Wrapping the text of a Kivy Label(包装 Kivy 标签的文本)
          <bdo id='O4kF6'></bdo><ul id='O4kF6'></ul>

            • <small id='O4kF6'></small><noframes id='O4kF6'>

              <i id='O4kF6'><tr id='O4kF6'><dt id='O4kF6'><q id='O4kF6'><span id='O4kF6'><b id='O4kF6'><form id='O4kF6'><ins id='O4kF6'></ins><ul id='O4kF6'></ul><sub id='O4kF6'></sub></form><legend id='O4kF6'></legend><bdo id='O4kF6'><pre id='O4kF6'><center id='O4kF6'></center></pre></bdo></b><th id='O4kF6'></th></span></q></dt></tr></i><div id='O4kF6'><tfoot id='O4kF6'></tfoot><dl id='O4kF6'><fieldset id='O4kF6'></fieldset></dl></div>
              <tfoot id='O4kF6'></tfoot>

                  <tbody id='O4kF6'></tbody>
                1. <legend id='O4kF6'><style id='O4kF6'><dir id='O4kF6'><q id='O4kF6'></q></dir></style></legend>
                  本文介绍了包装 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 标签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)

                    <bdo id='M92rz'></bdo><ul id='M92rz'></ul>

                      <i id='M92rz'><tr id='M92rz'><dt id='M92rz'><q id='M92rz'><span id='M92rz'><b id='M92rz'><form id='M92rz'><ins id='M92rz'></ins><ul id='M92rz'></ul><sub id='M92rz'></sub></form><legend id='M92rz'></legend><bdo id='M92rz'><pre id='M92rz'><center id='M92rz'></center></pre></bdo></b><th id='M92rz'></th></span></q></dt></tr></i><div id='M92rz'><tfoot id='M92rz'></tfoot><dl id='M92rz'><fieldset id='M92rz'></fieldset></dl></div>
                        <tbody id='M92rz'></tbody>
                      • <tfoot id='M92rz'></tfoot>
                      • <legend id='M92rz'><style id='M92rz'><dir id='M92rz'><q id='M92rz'></q></dir></style></legend>

                        <small id='M92rz'></small><noframes id='M92rz'>