python - 标签 : Entering long text onto screen

标签 python anaconda kivy spyder kivy-language

我正在尝试使用 python 的 kivy 库将带有编号点和段落的非常长的文本显示到我的应用程序的屏幕上。但是,我在这方面没有成功,因为我使用的 Label 属性只允许一行代码。如果我能得到一些关于我应该在代码中实现什么的指示,那就太好了!我正在考虑将长文本放入文本文件中并从那里读取它,但不太确定如何做。我想在应用程序中输入长文本的地方是“我能做什么”->“如何预防心脏病发作”

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. 
"""

最佳答案

试试这个:

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/

关于python - 标签 : Entering long text onto screen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55307278/

相关文章:

python - SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败

python - Pandas 不匹配的 conda 版本

python - Spyder 5.0.5 (MacOS) 上没有 Spyder 终端

python-3.x - 是否可以使用依赖项完全脱机进行conda安装?

python - 当我尝试通过单击图像按钮打开文件选择器时发生错误

python - 尝试在 Django 1.9 中迁移——奇怪的 SQL 错误 "django.db.utils.OperationalError: near ")": syntax error"

python - Python 中的十六进制到物理变量

python - 如何从cgi脚本获取python os.environ参数?

android - ./distribute.sh -1 上的 Kivy Buildozer 权限被拒绝

python-3.x - Kivy中如何防止widget的大小改变?