How to change a bg of ScrollView in my code and print the text on it?

Question:

My program parses the site at the click of a button and displays the text of the book on the user’s screen. But since I’m a beginner, I can’t find the ScrollView argument that would repaint it in the color I want. Maybe I’m doing something wrong, I would be glad if someone could tell me how to display the text on the screen ScrollView, which is not black!

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from bs4 import BeautifulSoup
from kivy.uix.scrollview import ScrollView
import requests

class MyButton(Button):
    color = (0, 0, 0, 1)
    valign = 'bottom'
    padding_y = 10
    background_color = (.93, .91, .67, 1)
    background_normal = ''


class Box(BoxLayout):
    orientation = "vertical"
    padding = [5,5,5,5]
    spacing = 10

    def on_kv_post(self, widget):

        self.add_widget(MyButton(text='И. С. Тургенев. «Отцы и дети»', on_press=self.btn_press))

    def btn_press(self, instance):
        self.clear_widgets()
        sc = ScrollView()
        x = 1
        data = ''
        while True:
            if x == 1:
                url = "http://loveread.ec/read_book.php?id=12021&p=1"
            elif x < 4:
                url = "http://loveread.ec/read_book.php?id=12021&p=" + f'{x}'
            else:
                break
            request = requests.get(url)
            soup = BeautifulSoup(request.text, "html.parser")
            teme = soup.find_all("p", class_="MsoNormal")
            for temes in teme:
                data += temes.text
            x = x + 1
        sc.add_widget(Label(text=f'{data}',color = (1,1,1,1)))
        self.add_widget(sc)


class MyApp(App):

    def build(self):
        return Box()


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


Asked By: NewbieTM

||

Answers:

You need your Label to adjust according to its text. The easiest way to do that is by using the kivy language. Here is one way to do it:

Add a new class MyLabel:

class MyLabel(Label):
    pass

Use the new class in your python:

sc.add_widget(MyLabel(text=f'{data}',color = (1,1,1,1)))

Add use the kivy language to define the new class:

class MyApp(App):

    def build(self):
        Builder.load_string('''
<MyLabel>:
    size_hint: 1, None
    text_size: self.width, None
    height: self.texture_size[1]
        ''')
        return Box()
Answered By: John Anderson
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.