kivy scrollview is not working

2019-09-15 17:04发布

问题:

I am trying to use kivy scrollview inside of EmployeeScreen class. it will not scroll!? what am I doing wrong? I hope this is not a duplicate, please help. I went to this link Kivy ScrollView - Not Scrolling. which seems to be the only question relating to kivy scrollview not scrolling. this didn't solve my problem.

.py file:

`from kivy.app import App

from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.scrollview import ScrollView


from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout


class LogInScreen(Screen):
    pass
class EmployeeScreen(Screen):
    pass

class Manager(ScreenManager):
    login_screen = ObjectProperty(None)
    employee_screen = ObjectProperty(None)


class CptApp(App):
    icon = 'Images\login\cptlogo.png'
    title = 'CPT'
    def build(self):
        return Manager()


if __name__=='__main__':
    CptApp().run()`

.kv file:

<Manager>:
id: screen_manager

login_screen: login_screen
employee_screen: employee_screen


LogInScreen:
    id: login_screen
    name: 'login'
    manager: screen_manager

    FloatLayout:
        StackLayout:
            orientation: 'lr-tb'
            canvas:
                Color:
                    rgba: 1,1,1,1
                Rectangle:
                    pos: self.pos
                    size: self.size
            Image:
                size_hint_y: .1
                source: 'Images\login\cptbanner.jpg'
                allow_stretch: True
                keep_ratio: True

            Image: 
                source: 'Images\login\HD7Brw.jpg'
                allow_stretch: True
                keep_ratio: False

    Label:
        size_hint_y: .05
        size_hint_x: .5
        pos_hint: {"x": .25, "y": .7}
        markup: True
        text: '[i][b][color=#000000]USER NAME[/color][/b][/i]'

    TextInput:
        id: 'username_input'
        multiline: False
        size_hint_x: .4
        size_hint_y: .05
        pos_hint: {"x": .3, "y": .65}

    Label:
        size_hint_y: .05
        size_hint_x: .5
        markup: True
        text: '[i][b][color=#000000]PASSWORD[/color][/b][/i]'
        pos_hint: {'x': .25, 'y': .5}

    TextInput:
        id: 'password_input'
        multiline: False
        password: True
        size_hint_x: .4
        size_hint_y: .05
        pos_hint: {'x': .3, 'y': .45}

    Image:
        source: 'Images/login/loginbutton.png'
        size_hint_x: .25
        size_hint_y: .1
        pos_hint: {'x': .375, 'y': .25}

    Button:
        id: 'login_button'
        background_color: 0,0,0,0
        markup: True
        text: '[i][b][color=#000000]LOGIN[/color][/b][/i]'
        size_hint_x: .25
        size_hint_y: .1
        pos_hint: {'x': .375, 'y': .25} 
        on_release: screen_manager.current = 'employeescreen'

EmployeeScreen:
    id: employee_screen
    name: 'employeescreen'
    manager: screen_manager

    StackLayout:
        orientation: 'lr-tb'
        canvas:
            Color:
                rgba: 1,1,1,1
            Rectangle:
                pos: self.pos
                size: self.size
        Image:
            size_hint_y: .1
            source: 'Images\login\cptbanner.jpg'
            allow_stretch: True
            keep_ratio: True

        ScrollView:
            do_scroll_x: False
            size: root.size
            pos: root.pos
            GridLayout:
                cols: 2
                size_hint_y: None
                height: self.minimum_height
                pos: root.pos

                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    size_hint_x: 1
                    size_hint_y: None
                    text: 'TEST'

回答1:

I was trying to use GridLayout in my .kv file, wrong, I had to create a class and override its init.

class MyLayout(GridLayout):
    def __init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
        self.size_hint_y = (None)
        self.bind(minimum_height = self.setter('height'))

then I placed the class in the .kv file where GridLayout was.