The following is a problem I have with the kivy module. When I created my first 3-4 widgets of the FirstScreen class - everything was fine, but after that the widgets started to occupy the same space that they used to, without expanding up. Maybe it's a problem of the ScrollView. There is no kv file the widgets are shown here
from functools import partial
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen, ScreenManager, NoTransition
from kivy.uix.scrollview import ScrollView
from kivy.uix.textinput import TextInput
class YogaApp(App):
def build(self):
self.sm = ScreenManager(transition=NoTransition())
self.sm.add_widget(FirstScreen(name='First'))
self.sm.add_widget(SecondScreen(name='Second'))
self.sm.add_widget(ThirdScreen(name='Third'))
self.sm.add_widget(FourthScreen(name='Fourth'))
self.sm.add_widget(FifthScreen(name='Fifth'))
# self.sm.current = 'Second' # Для того чтобы тестировать шаблоны. УБРАТЬ ПОТОМ
return self.sm
def change_screen_manager(self, screen):
self.sm.current = screen
class FirstScreen(Screen):
"""The first screen the user sees. It is the screen for creating a workout or a workout
template. It also shows all of your created templates which you can click for the second
screen to pop up
"""
total_training_sessions = 0
total_training_sessions_text = 'Всего тренировок: ' + str(total_training_sessions)
# Это шаблоны тренировок
templates = {}
# А это все упражнения
# exercises = []
def __init__(self, **kwargs):
"""
Creates all widgets and adds them on the first screen. Creates templates that
are a button with the text of the name and the exercises of the template.
It does so by extracting the template name and values from the dictionary 'templates'
:param kwargs:
"""
super(FirstScreen, self).__init__(**kwargs)
self.layout_root = ScrollView(do_scroll_x=False, do_scroll_y=True, size_hint=(1, None))
self.layout = BoxLayout(orientation='vertical', size_hint=(1, 1))
self.lbl1 = Label(text=self.total_training_sessions_text)
# Кнопка, при нажатии которой откроется второй экран
self.btn_create = Button(text='Создать Шаблон', on_press=partial(app.change_screen_manager, 'Second')) #Don't mind this. The partial function doesn't work
# self.btn_create.bind(on_press= изменить sm.current на второй экран)
self.lbl2 = Label(text='Шаблоны:') # , pos_hint={'x': -0.42}
self.layout.add_widget(self.lbl1)
self.layout.add_widget(self.btn_create)
self.layout.add_widget(self.lbl2)
import os
for filename in os.listdir('templates'):
with open(os.path.join('templates', filename), 'r', encoding='utf-8') as file:
self.lbl_name = Label(text = str(filename.replace('.txt', '')))
self.layout.add_widget(self.lbl_name)
k=0 #Считает кол-во упражнений в шаблоне
while True:
exercise_info = file.readlines(2)
k+=1
if not exercise_info:
break
num_of_exercise, exercise = exercise_info
num_of_exercise = str(num_of_exercise.replace("\n", ""))
exercise = str(exercise.replace("\n", ""))
text_1 = str(k) + " - " + exercise + " x" + num_of_exercise
self.template_lbl = Label(text = text_1)
self.layout.add_widget(self.template_lbl)
self.template_btn = Button() # will actually make the button when the problem is fixed
self.layout_root.add_widget(self.layout)
self.add_widget(self.layout_root)
def create_widgets(self):
for template in self.templates:
pass
class SecondScreen(Screen):
def __init__(self, **kwargs): # add template
super(SecondScreen, self).__init__(**kwargs)
self.create_widgets() #ТОЖЕ УБРАТЬ
def create_widgets(self):#, template):
# for x in template.keys():
# name = str(x)
name = 't1'
layout_root = ScrollView(do_scroll_x=False, do_scroll_y=True, size_hint=(1, None))
layout = BoxLayout(orientation='vertical')
lbl1 = Label(text=name)
btn_rest = Button()
btn_start = Button()
# btn_cancel = CancelButton()
# self.btn_cancel = Button(text='Отмена', on_press=app.sm.switch_to('First'))
# self.ids['cancel'] = Button()
# self.ids['layout'].add_widget(self.btn_start) НЕ РАБОТАЕТ
# self.ids['layout'].add_widget(self.btn_rest)
layout.add_widget(lbl1)
layout.add_widget(btn_rest)
layout.add_widget(btn_start)
# layout.add_widget(btn_cancel)
layout_root.add_widget(layout)
self.add_widget(layout_root)
class ThirdScreen(Screen):
def __init__(self, **kwargs):
super(ThirdScreen, self).__init__(**kwargs)
self.layout_root = ScrollView(do_scroll_x=False, do_scroll_y=True, size_hint=(1, None))
self.layout = BoxLayout(orientation='vertical')
self.name_input = TextInput(text='Название Шаблона', multiline=False, size_hint = (0.8, None), height='100dp')
class FourthScreen(Screen):
def __init__(self, **kwargs):
super(FourthScreen, self).__init__(**kwargs)
class FifthScreen(Screen):
def __init__(self, **kwargs):
super(FifthScreen, self).__init__(**kwargs)
if __name__ == '__main__':
app = YogaApp()
app.run()
I have tried giving pos_hint = {'top': 1} to the first widget. Didn't work (probably should have expected that) I thought that the BoxLayout doesn't occupy all of the space of the ScrollView, so I have given the BoxLayout size_hint = (1,1), but it didn't work
I have not tried anything else because I have no idea what's wrong with it
When you use a ScrollView
, its child widget (BoxLayout
in your case) must adjust its size to contain its children. The BoxLayout
has a minimum_height
property that calculates the minimum height of the BoxLayout
that will contain all its children. So that can be used to set the height
of the BoxLayout
like this:
self.layout = BoxLayout(orientation='vertical', size_hint=(1, None))
self.layout.bind(minimum_height=self.layout.setter('height'))
Note the size_hint_y=None
for the BoxLayout
, which allows the height
of the BoxLayout
to be adjusted. The second line above actually does the adjusting of the height
of the BoxLayout
.
One other issue is that in order for the BoxLayout
to calculate its minimum_height
, its children must have known heights (i.e., you cannot use size_hint
). An easy fix for that issue is to add size_hint_y=None, height=20
to each Widget
that gets added to the BoxLayout
.
Try making these changes to your code.