問題描述
我在嘗試使動態生成的標簽堆棧可滾動時遇到一些問題.我可能誤解了應該使用 ScrollView 的方式,所以我希望有人可以為我澄清一下.下面的代碼從一個csv中讀取一堆數據,當那個代碼顯示出來的時候,如果有很多數據,程序基本上會嘗試把所有的text/Labels壓縮到GridLayout中.我希望數據可以滾動.這是代碼的抽象版本:
I'm having some issues using the ScollView Widget in an attempt to make a dynamically-generated stack of labels scrollable. It's possible that I'm misunderstanding the way ScrollView should be utilized, so I'm hoping somebody could clarify it for me. The following code reads a bunch of data from a csv, and when that code is displayed, if there is a lot of data, the program will basically try to compress all the text/Labels into the GridLayout. I'd like the data to be scrollable. Here's an abstracted version of the code:
class showData(Screen):
def __init__(self, **kwargs):
super(showData, self).__init__(**kwargs)
self.my_data = read_csv_to_dict()
self.data_exists = 0 if len(self.my_data) == 0 else 1
### Create Widgets ###
layout_main = BoxLayout(orientation = 'vertical')
layout_back_button = BoxLayout(padding = [0, 0, 0, 20])
self.layout_data = GridLayout(cols = 3 if self.data_exists else 1)
self.scrollview_data = ScrollView()
button_back = Button(text = 'Main menu')
### Add widgets ###
self.add_widget(layout_main)
layout_main.add_widget(layout_back_button)
layout_main.add_widget(self.scrollview_data)
layout_back_button.add_widget(button_back)
if self.data_exists:
self.layout_data.add_widget(Label(text = 'label 1'))
self.layout_data.add_widget(Label(text = 'label 2'))
self.layout_data.add_widget(Label(text = 'label 3'))
self.display_data(self)
self.scrollview_data.add_widget(self.layout_data)
else:
self.scrollview_data.add_widget(Label(text = 'Records are empty'))
### Create button bindings ###
button_back.bind(on_press = switch_screen_to_main)
def display_data(obj, self):
data_dictReader = read_csv_to_dictReader()
for data_row in data_dictReader:
for value in data_row.values():
self.layout_data.add_widget( Label( text = value))
GridLayout/data 不可滾動.有人可以告訴我如何修復上面的代碼以使其可滾動嗎?謝謝你.
The GridLayout/data is not scrollable. Can someone tell me how to fix the code above to make it scrollable? Thank you.
推薦答案
你不見了 Kivy 文檔中的一些內容 在您的 GridLayout 上.它們是確保 GridLayout 大到可以滾動"的必要條件:
You are missing a few things from the Kivy Documentation on your GridLayout. They are necessary to make sure that the GridLayout is "big enough to scroll":
- 您必須確保將
size_hint_y
設置為None
,因為在這種情況下默認的1
不方便 - 將
GridLayout
的minimum_height
綁定到layout.setter('height')
. - 確保
ScrollView
具有適合滾動的大小
- You got to be sure that you set the
size_hint_y
toNone
, because the default1
is not convenient in this case - Bind the
minimum_height
of theGridLayout
tolayout.setter('height')
. - Be sure that the
ScrollView
has the right size to accommodate the scroll
這個例子和你在文檔中找到的差不多:
This example is pretty much what you find in the documentation:
from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class Example(App):
def build(self):
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
# Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))
for i in range(30):
btn = Button(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
root = ScrollView()
root.add_widget(layout)
return root
if __name__ == '__main__':
Example().run()
在本例中,ScrollView
是 Window
的大小,但您可以使用 size_hint
和 size
屬性.
In this example, the ScrollView
is the size of the Window
but you can manipulate it with the size_hint
and size
properties.
這篇關于ScrollView 小部件在 kivy 中不滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!