【问题标题】:Python Kivy: ScrollView not scrollable within FloatLayoutPython Kivy:ScrollView 在 FloatLayout 中不可滚动
【发布时间】:2020-12-18 22:43:37
【问题描述】:

我有一个基于 Tech With Tim 的 Kivy 教程应用程序的 FloatLayout。 FloatLayout 允许在任何设备上调整窗口大小,因为此应用程序将在 Android 和 iOS 上使用。 我已经想出了如何将 gridlayout 和 scrollview 放置在 floatlayout 中并将其放置在我想要的位置,但我需要能够滚动浏览红色项目列表。 (见图)

现在,它响应滚动,但不会一直滚动到列表中并停止或完全停留。项目按钮列表也延伸到视图之外并且从不压缩。不过,不确定这是否可以解决。

Image of GUI

代码:

class MainWindow(Screen):

    def sendData(self, data):
        pass

    def logOut(self):
        sm.current = "login"

    def on_enter(self):
        sundries = Sundry(join('Sundries.xlsx'))
        supplies = sundries.get_supplies()
        titleBtn = Button(text=supplies[0], size_hint=(0.4, 0.2), pos_hint={'x': 0,'y':0.8}, background_color=(0,0,255,1))

        root = ScrollView(size_hint=(0.4,0.8))
        layout = GridLayout(cols=1, spacing=1, size_hint=(0.8,0.7))
        # Make sure the height is such that there is something to scroll.
        for i in range(1, len(supplies), 1):
            btn = Button(text=supplies[i], size_hint_y=None, height=40,background_color=(255,0,0,1))
            layout.add_widget(btn)

        root.add_widget(layout)
        self.add_widget(root)
        self.add_widget(titleBtn)

如果你能帮我解决这个问题,真的很感激你的时间:)

【问题讨论】:

    标签: python python-3.x kivy


    【解决方案1】:

    ScrollView 的子项(用于垂直滚动)通常需要具有可变高度,因此您希望 size_hint_y 为 None。我认为如果您更改 GridLayout 声明,您的代码将起作用:

        layout = GridLayout(cols=1, spacing=1, size_hint=(0.8,None))
        layout.bind(minimum_height=layout.setter('height'))
    

    这让GridLayout 的高度随着行的增加而增加。

    【讨论】:

    • 这很完美!非常感谢!
    猜你喜欢
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 2014-04-02
    相关资源
    最近更新 更多