2016-02-29 3 views
0

В приложении, которое я создаю У меня есть диспетчер экрана, который содержит несколько экземпляров экрана. В одном из них я хочу иметь 2 стандартных виджета и несколько кнопок, которые динамически добавляются пользователем. Если этих кнопок слишком много, пользователь должен иметь возможность прокручивать их, пока не найдет тот, который ему нужен.Невозможно правильно определить размер виджетов

Для этого я использую сетку, содержащую два объекта: другую схему сетки и прокрутку. Макет сетки отвечает за 2 стандартных виджета (текстовый ввод и кнопка). Функция scrollview отвечает за динамически добавленные кнопки.

Я хочу, чтобы часть прокрутки занимала большую часть окна (скажем, 75%), чтобы пользователь мог видеть кнопки более четко, а сетка с двумя стандартными виджетами должна занимать оставшуюся часть. Тем не менее, gridlayout заканчивается тем, что занимает большую часть окна для себя.

Вот кусок кода (предположим, что динамически добавленные кнопки прямо сейчас 15):

sm = ScreenManager() 

class scr1(Screen): 
    pass 

#layout that occupies the entire window. Everything else will be added on this 
glayout = GridLayout(cols=1) 

#layout that should occupy 25% of the window 
layout1 = GridLayout(cols=1,size_hint_y=0.25) 

#layout to be added on the scrollview 
layout2 = GridLayout(cols=1,size_hint_y=None) 
layout2.bind(minimum_height=layout2.setter('height')) 

#screen to hold the glayout 
screen1 = scr1(name = '1') 

#adding a couple of widgets to layout1 
layout1.add_widget(Button(text = 'hey')) 
layout1.add_widget(TextInput(text = 'hey')) 

#scroller that should occupy 75% of the window 
scroller = ScrollView(size_hint = (1,None)) 
scroller.add_widget(layout2) 

#adding buttons to be scrolled 
for i in range(15): 
    layout2.add_widget(Button(text=str(i),size_hint_y=None)) 

#adding scroller and layout1 to glayout and then the glayout to the screen 
glayout.add_widget(scroller) 
glayout.add_widget(layout1) 
screen1.add_widget(glayout) 

#adding the screen to the screen manager 
sm.add_widget(screen1)  

Я не очень хорошо знаком с системой позиционирования, что kivy использования, и я не знаю, как я должен решите эту проблему. Вот как выглядит программа при запуске программы. You can see that the first small part is the scrollview, and the rest is the gridlayout

ответ

0

Вы установили size_hint_y ScrollView на None.

from kivy.uix.screenmanager import Screen, ScreenManager 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.scrollview import ScrollView 
from kivy.uix.button import Button 
from kivy.uix.textinput import TextInput 
sm = ScreenManager() 

class scr1(Screen): 
    pass 

#layout that occupies the entire window. Everything else will be added on this 
glayout = GridLayout(cols=1) 

#layout that should occupy 25% of the window 
layout1 = GridLayout(cols=1,size_hint_y=0.25) 

#layout to be added on the scrollview 
layout2 = GridLayout(cols=1,size_hint_y=None) 
layout2.bind(minimum_height=layout2.setter('height')) 

#screen to hold the glayout 
screen1 = scr1(name = '1') 

#adding a couple of widgets to layout1 
layout1.add_widget(Button(text = 'hey')) 
layout1.add_widget(TextInput(text = 'hey')) 

#scroller that should occupy 75% of the window 
scroller = ScrollView(size_hint_y=.75) 
scroller.add_widget(layout2) 

#adding buttons to be scrolled 
for i in range(15): 
    layout2.add_widget(Button(text=str(i),height=70,size_hint_y=None)) 

#adding scroller and layout1 to glayout and then the glayout to the screen 
glayout.add_widget(scroller) 
glayout.add_widget(layout1) 
screen1.add_widget(glayout) 

#adding the screen to the screen manager 
sm.add_widget(screen1) 
from kivy.app import App 

class Ap(App): 
    def build(self): 
     return sm 

Ap().run() 
Смежные вопросы