Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Kivy] Why I have to click twice to scroll?
I have a following problem: I need to write an app, where I will show proper answers for every question. I wrote with kivy some code and I'm struggling with one thing. I created a page. There is a button for showing answers, but after one press I only see a part of my answers and I can't scroll. But, when I press a button second time, everything is good. Could you tell me why is that? How to repair it? I would like to see all answers after pressing a button once and be able to scroll.
I attach code below:

import kivy
from import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.scrollview import ScrollView
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.config import Config
Config.set('graphics', 'resizable', True)
import os
import sys

class MyApp(App):
    def build(self):
        self.screen_manager = ScreenManager()

        self.answers = Answers()
        screen = Screen(name = "Answers")

        return self.screen_manager

class Answers(GridLayout):
    def __init__(self, **kwargs):

        self.rows = 3

        self.label = Label(text = "Answers: ", font_size = 40)

        self.button = Button(text="Show answers")

        self.scroll = ScrollableLabel(height = Window.size[1]*0.75, size_hint_y = None)

    def showanswers(self, instance):
        f = open("text.txt", "r")
        lines = f.readlines()

        ScrollableLabel.update(self.scroll, lines)
        myapp.screen_manager.current = "Answers"

class ScrollableLabel(ScrollView):
    def __init__(self, **kwargs):
        self.layout = GridLayout(cols = 1,  size_hint_y = None)

        self.lines = Label(size_hint_x = 1, size_hint_y = None, text_size = (self.width, None))

        self.scroll_to_point = Label()


    def update(self, lines):
        self.lines.text = '\n'

        for i in range(len(lines)):
            self.lines.text += '\n ' +str(i+1) + ". " + lines[i]

        self.layout.height = self.lines.texture_size[1]
        self.lines.height = self.lines.texture_size[1]
        self.lines.text_size = (self.lines.width*0.75, None)


f = open("text.txt", 'a+')
for i in range(30):
    f.write("Important text \n")

myapp = MyApp()

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] How to place scroll bar correctly scratchmyhead 1 279 May-18-2020, 04:17 PM
Last Post: scratchmyhead
  Scroll frame with MouseWheel Nemesis 1 317 Mar-25-2020, 09:29 PM
Last Post: Nemesis
  Require scroll bars horizontal and vertical throughout the window tejgandhi 2 793 Jun-28-2019, 03:13 AM
Last Post: tejgandhi
  [Tkinter] Scroll Bars going backwards goofygoo 2 596 Jun-07-2019, 05:07 PM
Last Post: goofygoo
  [Tkinter] Scroll at cursor position lollo 4 2,694 Jan-31-2018, 11:33 PM
Last Post: lollo
  [Tkinter] Scroll bar height is not fixed with Text widget MeeranRizvi 2 8,042 Feb-06-2017, 12:24 PM
Last Post: MeeranRizvi

Forum Jump:

Users browsing this thread: 1 Guest(s)