问题
I have a simple app in which i would like to insert a scroll view of several buttons. So there is the base code, i want a scroll view inside the grid layout. PS: i have this error: Menu object has no attribute 'view'
what I would like to obtain:
debug.py:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
class AppScreenManager(ScreenManager):
def __init__(self, **kwargs):
super(AppScreenManager, self).__init__(**kwargs)
class Menu(Screen):
def __init__(self, **kwargs):
super(Menu, self).__init__(**kwargs)
base = ["element {}".format(i) for i in range(40)]
for element in base:
self.view.add_widget(Button(text=element, size=(40,40), size_hint=(1, None), background_color=(0.5, 0.5, 0.5, 1), color=(1,1,1,1)))
Builder.load_file("debug.kv")
class MyAppli(App):
def build(self):
return AppScreenManager()
if __name__ == '__main__':
MyAppli().run()
debug.kv:
#:kivy 1.9.1
<AppScreenManager>:
Menu:
<Menu>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size: (64, 64)
size_hint: (1, None)
Button:
text: "Menu"
color: (1,1,1,1)
background_color: (.3, .3, .3, 1)
GridLayout: # here i want a scrollview
id: view
cols: 1
回答1:
Kivy Language
Note that the outermost widget applies the kv rules to all its inner widgets before any other rules are applied. This means if an inner widget contains ids, these ids may not be available during the inner widget’s init function.
ScrollView » Managing the Content Size and Position
By default, the size_hint is (1, 1), so the content size will fit your ScrollView exactly (you will have nothing to scroll). You must deactivate at least one of the size_hint instructions (x or y) of the child to enable scrolling. Setting size_hint_min to not be None will also enable scrolling for that dimension when the ScrollView is smaller than the minimum size.
To scroll a GridLayout on it’s Y-axis/vertically, set the child’s width to that of the ScrollView (size_hint_x=1), and set the size_hint_y property to None:
Use Clock.schedule_once to invoke a new method, create_scrollview. Make sure the height is such that there is something to scroll layout.bind(minimum_height=layout.setter('height'))
. Please refer to the example below for details.
Example
debug.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.clock import Clock
class AppScreenManager(ScreenManager):
def __init__(self, **kwargs):
super(AppScreenManager, self).__init__(**kwargs)
class Menu(Screen):
view = ObjectProperty(None)
def __init__(self, **kwargs):
super(Menu, self).__init__(**kwargs)
Clock.schedule_once(self.create_scrollview)
def create_scrollview(self, dt):
base = ["element {}".format(i) for i in range(40)]
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
layout.bind(minimum_height=layout.setter("height"))
for element in base:
layout.add_widget(Button(text=element, size=(50, 50), size_hint=(1, None),
background_color=(0.5, 0.5, 0.5, 1), color=(1, 1, 1, 1)))
scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
scrollview.add_widget(layout)
self.view.add_widget(scrollview)
Builder.load_file("debug.kv")
class MyAppli(App):
def build(self):
return AppScreenManager()
if __name__ == '__main__':
MyAppli().run()
debug.kv
#:kivy 1.10.0
<AppScreenManager>:
Menu:
<Menu>:
view: view
BoxLayout:
orientation: 'vertical'
BoxLayout:
size: (64, 64)
size_hint: (1, None)
Button:
text: "Menu"
color: (1, 1, 1, 1)
background_color: (.3, .3, .3, 1)
ScrollView:
id: view
Output
回答2:
ScrollView:
size: self.size
GridLayout: # here i want a scrollview
id: view
cols: 1
size_hint_y: None
height: self.minimum_height
Button:
text:
size_hint_y: None
Do something like this and add the buttons needed in the gridlayout... I'm new to Kivy so please bear with me.
来源:https://stackoverflow.com/questions/46749491/kivy-python-scroll-view-in-a-layout