简体   繁体   中英

How can I align my Label text on the left and still have X-axis scroll in Kivy?

I am making a weather app in which text results are displayed in a scrollview. I have been testing my app and have decided I need to add in x-axis scrolling. In previous times I was making use of just y-axis scrolling but have found that some of the weather results include longer lines that end up not fitting in the screen width and as a result, are being moved down a line which makes the formatting look bad.

I have found that not having anything set in text_size: allows the words to run off the screen without having them forced onto the next line. That is what I needed in regards to that.

Now I am stuck trying to figure out how to have my text results start out being aligned on the left margin of the screen and then allowing the user to scroll across to view entire line(s) if necessary. I am planning on having the run on iPad and iPhone so the screen widths will obviously differ, and I don't want to make the font size any smaller because the user wont be able to read it.

I have tried a whole bunch of different combinations with the layouts in the scrollview, with the parent and child but can't seem to get it right.

I have made a very minimalistic reproducible version that seems to behave the same as my actual app. The scrollview in my homescreen is the culprit. If someone could please help me out with this, that would be so good, thank you.

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen

class HomeScreen(Screen):
    pass
class Results1Screen(Screen):
    pass

class TestApp(App):
    def build(self):
        return Builder.load_file("main.kv")

    def on_start(self):
        brief_res_1 = self.root.ids["home_screen"].ids["brief_res_1"]
        brief_res_1.text = (open("text.txt", "r")).read()

TestApp().run()

main.kv

#:include kv/homescreen.kv
GridLayout:
    cols: 1
    FloatLayout:
        ScreenManager:
            size_hint: 1, .95
            pos_hint: {"top": .95, "left": 1}
            id: screen_manager
            HomeScreen:
                name: "home_screen"
                id: home_screen

homescreen.kv

<HomeScreen>:
    BoxLayout:
        pos_hint: {"top": .7, "left": 1} 
        size_hint: 1, 1
        size_hint_y: None
        ScrollView:
            Label:
                id: brief_res_1
                font_size: '9sp' # need to keep this at '9sp'
                do_scroll_x: True
                size_hint_y: None
                size_hint_x: None
                height: self.texture_size[1]
                halign: "left"       # not too sure if this should be "left"
                valign: "center"
                text:

text.txt

LINE 1 TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
LINE 2 TEXT TEXT TEXT TEXT TEXT TEXT
LINE 3 TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT

You can just replace:

height: self.texture_size[1]

with:

size: self.texture_size

If you do not set the width of the Label , it will take the default width of 100 , and your x-scrolling will not happen until the width of your HomeScreen gets less than 100 pixels wide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM