【问题标题】:how to maximize scrollbar in Kivy python?如何在 Kivy python 中最大化滚动条?
【发布时间】:2018-01-04 10:48:18
【问题描述】:

我在 kivy 中使用 multiTab。如何放大滚动条以便可以通过鼠标移动(up_down)滚动条。我正在使用 BoxLayout 和 RecycleView。下面的代码使用的是 gridLayout,但不在 BoxLayout 中:

layout.bind(minimum_height=layout.setter('height'))

【问题讨论】:

  • 您实际上无法拖动该滚动条进行滚动,您需要一个单独的滑块来执行此操作。
  • 那么当我使用RecycleView 和BoxLayout 时该怎么做呢?你能举一些例子吗?谢谢。
  • 我想我在自己做的时候用过这个例子github.com/kivy/kivy/wiki/A-draggable-scrollbar-using-a-slider,试试看。

标签: python user-interface scrollview kivy


【解决方案1】:

这是一个如何扩展 ScrollView 以获得可拖动滑块的示例。

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.label import Label

Builder.load_string("""
<ScrollSlider>:
    ScrollView:
        id: scrlvw
        bar_width: 0
        GridLayout:
            id: grid
            size_hint_y:None
            cols:1
            height: self.minimum_height
            scroll_y: slider.value
            on_touch_move: slider.value = self.scroll_y
    Slider:
        size_hint_x: None
        width: root.width*0.2
        id: slider
        min: 0
        max: 1
        orientation: 'vertical'
        value: scrlvw.scroll_y
        on_value: scrlvw.scroll_y = self.value

""")


class ScrollSlider(BoxLayout):

    def custom_add(self, widget):
        self.ids.grid.add_widget(widget)

class MyApp(App):


    def build(self):
        scrollslider = ScrollSlider()
        for i in range(1, 100):
            scrollslider.custom_add(Label(text=str(i), height=100, size_hint_y=None))
        return scrollslider

if __name__ == '__main__':
    MyApp().run()

您需要使用custom_addScrollSlider中添加小部件或在kivy中将它们添加到GridLayout中。

我从@Edvardas Dlugauskas 提供的链接中获得了一些灵感。

您提供的代码不适用于BoxLayout,因为BoxLayout 只占用其父级的大小。为了能够滚动,您需要将 sth 添加到比 ScrollView 本身大的 ScrollView 中。

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2020-01-22
    • 2014-12-28
    • 2017-02-24
    相关资源
    最近更新 更多