我正在学习 kivy,我想从 kivymd 制作一组可滚动的按钮,为此我使用 MDScrollViewRefreshLayout(取自示例)。但是有些按钮是不可见的,可见的不滚动。我想看看使用 ScrollView 的最简单的例子,这样 python 代码和标记是分开的。我找到了一个对我来说很清楚的示例,但是没有单独的标记文件,我无法弄清楚如何获得所需的结果。
主文件
from kivy.factory import Factory
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.config import Config
from kivy.core.window import Window
Config.set('graphics', 'resizable', '0')
Window.size = (351, 512)
Builder.load_file('dictionary.kv')
class Basic(MDApp):
def __init__(self, **kvargs):
super(Basic, self).__init__(**kvargs)
self.main = Factory.MainMenu()
def build(self):
return self.main
Basic().run()
字典.kv:
MDScrollViewRefreshLayout:
do_scroll_x: False
size_hint_y: None
BoxLayout:
orientation: "vertical"
padding: 30
spacing: 20
size_hint_y: None
pos_hint: {"top": -.6}
MDTextButton:
text: 'game 1_'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 2'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 3'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: "game 4"
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 1'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 2'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 3'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: "game 4"
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 1'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 2'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 3'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: "game 4"
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 1'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 2'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: 'game 3'
heigh: dp(60)
size_hint_y: None
MDTextButton:
text: "game 4"
heigh: dp(60)
size_hint_y: None
解决方案
height: self.minimum_height
是BoxLayout
. 并且同时需要指定每个按钮的大小heigh: dp(60)
如果您对 ScrollView 有疑问,请检查以下内容:
size_hint_y: None
heigh: dp(60)
height: self.minimum_height
确保你有一些东西可以滚动,即小部件不适合屏幕。: