标签:在屏幕上输入长文本

时间:2019-03-22 20:22:23

标签: python anaconda kivy spyder kivy-language

我正在尝试使用kivy Python库在应用程序的屏幕上显示带有编号点和段落的超长文本。但是,由于使用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. 
"""

2 个答案:

答案 0 :(得分:0)

尝试一下:

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/

答案 1 :(得分:0)

您现有的Label代码可以处理长文本。如果长文本不经常更改,则可以将其放置在班级中。

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

输出

App Startup Scrolled to point 2 Scrolled to bottom of long text