如何使用 kivy 中的 ScrollView 函数制作 FloatLayout Scrollablle
How do I make a FloatLayout Scrollablle using the ScrollView function in kivy
我想弄清楚如何滚动浮动布局,该布局在各个地方都有小部件,但是当我将浮动布局添加到滚动视图时,它不会一直滚动。但是,当我使用网格布局执行此操作时,它似乎可以完美滚动。如果有人帮助我,我将不胜感激。
这是我的代码:
基维
WindowManager:
ProfileWindow:
<ProfileWindow>:
name: 'Profile'
canvas.before:
Color:
rgba: .94,1,.9, 1
Rectangle:
pos: self.pos
size: self.size
ScrollView:
size_hint_y: 1.4
pos_hint: {'x':0, 'y': .01}
do_scroll_x: False
do_scroll_y: True
FloatLayout:
size_hint_y: None
size_hint_x: None
size: root.width, root.height
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 1'
pos_hint: {'x':0.2, 'y':0.2}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 2'
pos_hint: {'x':0.2, 'y':-0.4}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 3'
pos_hint: {'x':0.2, 'y':-1.0}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 4'
pos_hint: {'x':0.2, 'y':-1.4}
colour: (1, 0, 0, 1)```
PYTHON 文件
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import csv
from kivy.uix.scrollview import ScrollView
from kivy.uix.carousel import Carousel
from kivy.effects.kinetic import KineticEffect
from kivy.base import runTouchApp
class ProfileWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
Build_LearnAB = Builder.load_file('sfl.kv')
class LearnScrollViewApp(App):
def build(self):
return Build_LearnAB
if __name__ == "__main__":
LearnScrollViewApp().run()
您在该代码中做了一些奇怪的事情。首先,使用大于 1
的 size_hint
值(就像您对 ScrollView
所做的那样)设置 ScrollView
的大小大于其容器。这意味着您将无法看到整个 ScrollView
.
同样,您将 pos_hint
值设置为小于 0
的数字。这会将 Buttons
定位在 FloatLayout
之外,因此它们将不可见。
我建议删除以下行:
size_hint_y: 1.4
来自 ScrollView
并且只使用默认值 1
。
然后,将 FloatLayout
的高度设置为大于 ScrollView
的高度。如果 ScrollView
的子项不大于自身,则 ScrollView
不会滚动。例如,将 FloatLayout
的高度设置为 ScrollView
高度的两倍:
size: root.width, root.height*2
然后,用pos_hint
将Buttons
放在FloatLayout
中,但保持0
和小于1
之间的值,放置FloatLayout
.
中的 Buttons
这是您的 kv
的修改版本,其中进行了以下更改:
WindowManager:
ProfileWindow:
<ProfileWindow>:
name: 'Profile'
canvas.before:
Color:
rgba: .94,1,.9, 1
Rectangle:
pos: self.pos
size: self.size
ScrollView:
# size_hint_y: 1.4
pos_hint: {'x':0, 'y': .01}
do_scroll_x: False
do_scroll_y: True
FloatLayout:
size_hint_y: None
size_hint_x: None
size: root.width, root.height*2
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 1'
pos_hint: {'x':0.2, 'y':0.75}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 2'
pos_hint: {'x':0.2, 'y':0.5}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 3'
pos_hint: {'x':0.2, 'y':0.25}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 4'
pos_hint: {'x':0.2, 'y':0}
colour: (1, 0, 0, 1)
我想弄清楚如何滚动浮动布局,该布局在各个地方都有小部件,但是当我将浮动布局添加到滚动视图时,它不会一直滚动。但是,当我使用网格布局执行此操作时,它似乎可以完美滚动。如果有人帮助我,我将不胜感激。
这是我的代码:
基维
WindowManager:
ProfileWindow:
<ProfileWindow>:
name: 'Profile'
canvas.before:
Color:
rgba: .94,1,.9, 1
Rectangle:
pos: self.pos
size: self.size
ScrollView:
size_hint_y: 1.4
pos_hint: {'x':0, 'y': .01}
do_scroll_x: False
do_scroll_y: True
FloatLayout:
size_hint_y: None
size_hint_x: None
size: root.width, root.height
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 1'
pos_hint: {'x':0.2, 'y':0.2}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 2'
pos_hint: {'x':0.2, 'y':-0.4}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 3'
pos_hint: {'x':0.2, 'y':-1.0}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 4'
pos_hint: {'x':0.2, 'y':-1.4}
colour: (1, 0, 0, 1)```
PYTHON 文件
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import csv
from kivy.uix.scrollview import ScrollView
from kivy.uix.carousel import Carousel
from kivy.effects.kinetic import KineticEffect
from kivy.base import runTouchApp
class ProfileWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
Build_LearnAB = Builder.load_file('sfl.kv')
class LearnScrollViewApp(App):
def build(self):
return Build_LearnAB
if __name__ == "__main__":
LearnScrollViewApp().run()
您在该代码中做了一些奇怪的事情。首先,使用大于 1
的 size_hint
值(就像您对 ScrollView
所做的那样)设置 ScrollView
的大小大于其容器。这意味着您将无法看到整个 ScrollView
.
同样,您将 pos_hint
值设置为小于 0
的数字。这会将 Buttons
定位在 FloatLayout
之外,因此它们将不可见。
我建议删除以下行:
size_hint_y: 1.4
来自 ScrollView
并且只使用默认值 1
。
然后,将 FloatLayout
的高度设置为大于 ScrollView
的高度。如果 ScrollView
的子项不大于自身,则 ScrollView
不会滚动。例如,将 FloatLayout
的高度设置为 ScrollView
高度的两倍:
size: root.width, root.height*2
然后,用pos_hint
将Buttons
放在FloatLayout
中,但保持0
和小于1
之间的值,放置FloatLayout
.
Buttons
这是您的 kv
的修改版本,其中进行了以下更改:
WindowManager:
ProfileWindow:
<ProfileWindow>:
name: 'Profile'
canvas.before:
Color:
rgba: .94,1,.9, 1
Rectangle:
pos: self.pos
size: self.size
ScrollView:
# size_hint_y: 1.4
pos_hint: {'x':0, 'y': .01}
do_scroll_x: False
do_scroll_y: True
FloatLayout:
size_hint_y: None
size_hint_x: None
size: root.width, root.height*2
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 1'
pos_hint: {'x':0.2, 'y':0.75}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 2'
pos_hint: {'x':0.2, 'y':0.5}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 3'
pos_hint: {'x':0.2, 'y':0.25}
colour: (1, 0, 0, 1)
Button:
size_hint_y: None
size_hint_x: None
height: 100
width: 100
text: 'test 4'
pos_hint: {'x':0.2, 'y':0}
colour: (1, 0, 0, 1)