【问题标题】:Label: Entering long text onto screen标签:在屏幕上输入长文本
【发布时间】:2019-03-22 20:22:23
【问题描述】:

我正在尝试使用 Python 的 kivy 库将带有编号点和段落的非常长的文本放到我的应用程序的屏幕上。但是,我没有成功,因为我使用的 Label 属性只允许一行代码。如果我能得到一些关于我应该在我的代码中实现什么的指针,那就太好了!我正在考虑将长文本放入一个文本文件并从那里读取它,但不太确定如何去做。我想在应用内输入长文本的地方是"What can I do" -> "How to prevent a heart attack"

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup

Builder.load_string("""
<Bump@Button>:
    font_size: 40
    color: 1,1,1,1
    size_hint: 0.8,0.4 
    background_color: 1,0,0,1

<Back@Button>: 
    text: 'back'
    pos_hint: {'x':0,'y':0.9}
    size_hint: 0.2,0.1

<menu>:

    Bump:
        text: "What can I do?"
        pos_hint: {'center_x': 0.5, 'center_y':0.7}
        on_press: root.manager.current = 'info'
        on_press: root.manager.transition.direction = 'left'
    Label:
        text: "Remity Biotechnologies"
        pos_hint: {'center_x': 0.5, 'center_y':0.95}

<info>:
    Bump:
        text: "How to prevent a heart attack?"
        size_hint:0.8,0.15
        pos_hint:{'center_x':0.5,'center_y':0.15}
        on_press: root.manager.current = 'info2'
        on_press: root.manager.transition.direction = 'left'
    Back:
        on_press: root.manager.current = 'menu'
        on_press: root.manager.transition.direction = 'right'
        on_press: root.manager.transition.direction = 'left'
<info2>
    ScrollView:
        do_scroll_x: False
        do_scroll_y: True
        bar_width: 4
        Label:                            #PROBLEM LOCATED HERE
            text: "THIS IS WHERE I WANT TO PUT A LONG TEXT"
            font_size: 90
            size_hint_x: 1.0               
            size_hint_y: None
            text_size: self.width, None
            height: self.texture_size[1]


    Back:
        on_press: root.manager.current = 'info'
        on_press: root.manager.transition.direction = 'right'       


""")     
class confirm(Popup):
    pass

class menu(Screen):
    def update(self,dt):
        pass

class info(Screen):
    def update(self,dt):
        pass

class info2(Screen):
    def update(self,dt):
        pass

class ScreenManager(ScreenManager):  
    def update(self,dt):
        self.current_screen.update(dt)

sm = ScreenManager()
sm.add_widget(menu(name='menu'))
sm.add_widget(info(name='info'))
sm.add_widget(info2(name='info2'))


class SimpleKivy4(App):

    def build(self):
        return sm

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

我要输入的文字是……

long_text =\

"""1)   Be more physically active
Consult with your doctor on the type of exercise you can do and attempt to be consistent with your work outs, with 150 minutes of physical activity every week. Some simple exercises we can recommend are brisk walking, cycling, squats, tow and chair stands, and etc. 
2)  Quit smoking
Smoking is the leading cause of preventable death. It damages the artery walls which increases the chances of atrial fibrillation, strokes and even cancer. Put it out before it puts you out. 
3)  Don’t drink a lot of alcohol
Drinking alcohol raises your blood pressure and severely damages the cardiovascular system. Men should bring no more than 2 drinks a day and women only one. One drink represents 
-One 12-ounce can or bottle of regular beer, ale, or wine cooler
-One 8- or 9-ounce can or bottle of malt liquor
-One 5-ounce glass of red or white wine
-One 1.5-ounce shot glass of distilled spirits like gin, rum, tequila, vodka, or whiskey

4)  Healthy diet
Start eating foods that are low in trans and saturated fats, added sugars and salt. Eat more fruits, vegetables, whole grains, and highly fibrous foods. You should aim for a healthy weight by having smaller portion sizes. 
5)  Manage stress
Stress itself can increase blood pressure and blood clots which increases the chances of heart related problems. Learn to manage your stress levels by doing meditation or physical activities. 
"""

【问题讨论】:

    标签: python anaconda kivy spyder kivy-language


    【解决方案1】:

    试试这个:

    Label:
        size_hint_y: None
        text_size: self.width, None
        height: self.texture_size[1]
    

    关于它的来源/博客文章:https://blog.kivy.org/2014/07/wrapping-text-in-kivys-label/

    【讨论】:

      【解决方案2】:

      您现有的标签代码可以处理长文本。如果不经常更改,您可以将长文本放在类中。

      Python 脚本

      在类 info2 中声明 long_text:

      py - 片段

      from kivy.properties import StringProperty
      ...
      class info2(Screen):
          long_text = StringProperty('')
      
          def __init__(self, **kwargs):
              super(info2, self).__init__(**kwargs)
              self.long_text = """
              1)   Be more physically active
              Consult with your doctor on the type of exercise you can do and attempt to be consistent with your work outs, with 150 minutes of physical activity every week. Some simple exercises we can recommend are brisk walking, cycling, squats, tow and chair stands, and etc. 
              2)  Quit smoking
              Smoking is the leading cause of preventable death. It damages the artery walls which increases the chances of atrial fibrillation, strokes and even cancer. Put it out before it puts you out. 
              3)  Don’t drink a lot of alcohol
              Drinking alcohol raises your blood pressure and severely damages the cardiovascular system. Men should bring no more than 2 drinks a day and women only one. One drink represents 
              -One 12-ounce can or bottle of regular beer, ale, or wine cooler
              -One 8- or 9-ounce can or bottle of malt liquor
              -One 5-ounce glass of red or white wine
              -One 1.5-ounce shot glass of distilled spirits like gin, rum, tequila, vodka, or whiskey
      
              4)  Healthy diet
              Start eating foods that are low in trans and saturated fats, added sugars and salt. Eat more fruits, vegetables, whole grains, and highly fibrous foods. You should aim for a healthy weight by having smaller portion sizes. 
              5)  Manage stress
              Stress itself can increase blood pressure and blood clots which increases the chances of heart related problems. Learn to manage your stress levels by doing meditation or physical activities. 
              """
      

      kv文件

      • 始终在切换屏幕之前先更改 ScreenManager 过渡方向。
      • text: "THIS IS WHERE I WANT TO PUT A LONG TEXT" 替换为text: root.long_text

      kv - 片段

          on_press: 
              root.manager.transition.direction = 'left'
              root.manager.current = 'info'
          ...
      
      Label:
          text: root.long_text
      

      示例

      main.py

      ​​>
      from kivy.lang import Builder
      from kivy.app import App
      from kivy.uix.screenmanager import ScreenManager, Screen
      from kivy.uix.popup import Popup
      from kivy.properties import StringProperty
      
      Builder.load_string("""
      <Bump@Button>:
          font_size: 40
          color: 1,1,1,1
          size_hint: 0.8,0.4 
          background_color: 1,0,0,1
      
      <Back@Button>: 
          text: 'back'
          pos_hint: {'x':0,'y':0.9}
          size_hint: 0.2,0.1
      
      <menu>:
      
          Bump:
              text: "What can I do?"
              pos_hint: {'center_x': 0.5, 'center_y':0.7}
              on_press: 
                  root.manager.transition.direction = 'left'
                  root.manager.current = 'info'
          Label:
              text: "Remity Biotechnologies"
              pos_hint: {'center_x': 0.5, 'center_y':0.95}
      
      <info>:
          Bump:
              text: "How to prevent a heart attack?"
              size_hint:0.8,0.15
              pos_hint:{'center_x':0.5,'center_y':0.15}
              on_press: 
                  root.manager.transition.direction = 'left'
                  root.manager.current = 'info2'
          Back:
              on_press: 
                  root.manager.transition.direction = 'right'
                  root.manager.current = 'menu'
      <info2>:
          ScrollView:
              do_scroll_x: False
              do_scroll_y: True
              bar_width: 4
      
              Label:
                  text: root.long_text
                  font_size: 90
                  size_hint_x: 1.0               
                  size_hint_y: None
                  text_size: self.width, None
                  height: self.texture_size[1]
      
          Back:
              on_press: 
                  root.manager.transition.direction = 'right'       
                  root.manager.current = 'info'
      
      
      """)
      
      
      class confirm(Popup):
          pass
      
      
      class menu(Screen):
          def update(self, dt):
              pass
      
      
      class info(Screen):
          def update(self, dt):
              pass
      
      
      class info2(Screen):
          long_text = StringProperty('')
      
          def __init__(self, **kwargs):
              super(info2, self).__init__(**kwargs)
              self.long_text = """
              1)   Be more physically active
              Consult with your doctor on the type of exercise you can do and attempt to be consistent with your work outs, with 150 minutes of physical activity every week. Some simple exercises we can recommend are brisk walking, cycling, squats, tow and chair stands, and etc. 
              2)  Quit smoking
              Smoking is the leading cause of preventable death. It damages the artery walls which increases the chances of atrial fibrillation, strokes and even cancer. Put it out before it puts you out. 
              3)  Don’t drink a lot of alcohol
              Drinking alcohol raises your blood pressure and severely damages the cardiovascular system. Men should bring no more than 2 drinks a day and women only one. One drink represents 
              -One 12-ounce can or bottle of regular beer, ale, or wine cooler
              -One 8- or 9-ounce can or bottle of malt liquor
              -One 5-ounce glass of red or white wine
              -One 1.5-ounce shot glass of distilled spirits like gin, rum, tequila, vodka, or whiskey
      
              4)  Healthy diet
              Start eating foods that are low in trans and saturated fats, added sugars and salt. Eat more fruits, vegetables, whole grains, and highly fibrous foods. You should aim for a healthy weight by having smaller portion sizes. 
              5)  Manage stress
              Stress itself can increase blood pressure and blood clots which increases the chances of heart related problems. Learn to manage your stress levels by doing meditation or physical activities. 
              """
      
          def update(self, dt):
              pass
      
      
      class ScreenManager(ScreenManager):
      
          def update(self, dt):
              self.current_screen.update(dt)
      
      
      sm = ScreenManager()
      sm.add_widget(menu(name='menu'))
      sm.add_widget(info(name='info'))
      sm.add_widget(info2(name='info2'))
      
      
      class SimpleKivy4(App):
      
          def build(self):
              return sm
      
      
      if __name__ == "__main__":
          SimpleKivy4().run()
      

      输出

      【讨论】:

        猜你喜欢
        • 2019-05-13
        • 2021-11-10
        • 2015-12-10
        • 2018-05-25
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        • 2012-05-23
        • 2016-07-18
        相关资源
        最近更新 更多