ScrollView 不允许我滚动
ScrollView not allowing me to scroll
我一直在开发我的第一个 kivy 应用程序来了解和理解这门语言。我目前坚持让我的 ScrollView 真正滚动。我已经查找了有关该主题的许多不同答案和视频。下面是我当前的 ScrollView 脚本。谁能帮我解决这个问题?
class SearchMovie(GridLayout):
global MovieList
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 1
scrolly = ScrollView(size_hint=(1, None), size=(Window.width, Window.height*.9))
subgrid = GridLayout(cols=1, spacing=20)
movlist = MovieList[0]["Box1"]
nmovlist = []
for i in movlist:
nmovlist.append(i)
nmovlist.sort()
for i in nmovlist:
movlab = Label(text=i)
subgrid.add_widget(movlab)
scrolly.add_widget(subgrid)
self.add_widget(scrolly)
self.goback = Button(text="Go Back", background_color =[0, 0, 1, 1], pos_hint={'bottom':1, 'center_x':1})
self.goback.bind(on_press=self.go_back)
self.add_widget(self.goback)
def go_back(self, instance):
MovieScript.screen_manager.current = "Main Page"
如有任何帮助,我们将不胜感激!
您必须设置 subgrid
的 height
。默认值为 size_hint=(1,1)
,这使得 subgrid
与 ScrollView
的大小相同,因此没有可滚动的内容。这是您的代码的修改版本:
class SearchMovie(GridLayout):
global MovieList
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 1
scrolly = ScrollView(size_hint=(1, None), size=(Window.width, Window.height*.9))
subgrid = GridLayout(cols=1, spacing=20, size_hint_y=None) # use size_hint_y here
movlist = MovieList[0]["Box1"]
nmovlist = []
for i in movlist:
nmovlist.append(i)
nmovlist.sort()
label_height = 40 # this will be the height of each Label
total_height = 0 # this will be the total height of the subgrid
for i in nmovlist:
movlab = Label(text=i, size_hint_y=None, height=label_height) # set height
total_height += label_height + 20 # sum heights (plus spacing)
subgrid.add_widget(movlab)
subgrid.height = total_height # set actual height of subgrid
scrolly.add_widget(subgrid)
self.add_widget(scrolly)
self.goback = Button(text="Go Back", background_color =[0, 0, 1, 1], pos_hint={'bottom':1, 'center_x':1})
self.goback.bind(on_press=self.go_back)
self.add_widget(self.goback)
def go_back(self, instance):
MovieScript.screen_manager.current = "Main Page"
另一种方法是使用 kivy 语言。例如,您可以为 SearchMovie
class:
制定规则
kv = '''
<SearchMovie>:
cols: 1
ScrollView:
size_hint: 1, 0.9
GridLayout:
id: grid
cols: 1
size_hint_y: None
height: self.minimum_height
Button:
size_hint: 1, 0.1
text: "Go Back"
background_color: [0, 0, 1, 1]
pos_hint: {'bottom': 1, 'center_x': 1}
on_press: root.go_back()
'''
那么你的实际SearchMovie
class可以是:
class SearchMovie(GridLayout):
global MovieList
def __init__(self, **kwargs):
super().__init__(**kwargs)
subgrid = self.ids.grid
movlist = MovieList[0]["Box1"]
nmovlist = []
for i in movlist:
nmovlist.append(str(i))
nmovlist.sort()
for i in nmovlist:
movlab = Label(text=i, size_hint_y=None, height=40)
subgrid.add_widget(movlab)
def go_back(self, instance):
MovieScript.screen_manager.current = "Main Page"
请务必在创建 SearchMovie
实例之前加载上述 kv
字符串。
我一直在开发我的第一个 kivy 应用程序来了解和理解这门语言。我目前坚持让我的 ScrollView 真正滚动。我已经查找了有关该主题的许多不同答案和视频。下面是我当前的 ScrollView 脚本。谁能帮我解决这个问题?
class SearchMovie(GridLayout):
global MovieList
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 1
scrolly = ScrollView(size_hint=(1, None), size=(Window.width, Window.height*.9))
subgrid = GridLayout(cols=1, spacing=20)
movlist = MovieList[0]["Box1"]
nmovlist = []
for i in movlist:
nmovlist.append(i)
nmovlist.sort()
for i in nmovlist:
movlab = Label(text=i)
subgrid.add_widget(movlab)
scrolly.add_widget(subgrid)
self.add_widget(scrolly)
self.goback = Button(text="Go Back", background_color =[0, 0, 1, 1], pos_hint={'bottom':1, 'center_x':1})
self.goback.bind(on_press=self.go_back)
self.add_widget(self.goback)
def go_back(self, instance):
MovieScript.screen_manager.current = "Main Page"
如有任何帮助,我们将不胜感激!
您必须设置 subgrid
的 height
。默认值为 size_hint=(1,1)
,这使得 subgrid
与 ScrollView
的大小相同,因此没有可滚动的内容。这是您的代码的修改版本:
class SearchMovie(GridLayout):
global MovieList
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 1
scrolly = ScrollView(size_hint=(1, None), size=(Window.width, Window.height*.9))
subgrid = GridLayout(cols=1, spacing=20, size_hint_y=None) # use size_hint_y here
movlist = MovieList[0]["Box1"]
nmovlist = []
for i in movlist:
nmovlist.append(i)
nmovlist.sort()
label_height = 40 # this will be the height of each Label
total_height = 0 # this will be the total height of the subgrid
for i in nmovlist:
movlab = Label(text=i, size_hint_y=None, height=label_height) # set height
total_height += label_height + 20 # sum heights (plus spacing)
subgrid.add_widget(movlab)
subgrid.height = total_height # set actual height of subgrid
scrolly.add_widget(subgrid)
self.add_widget(scrolly)
self.goback = Button(text="Go Back", background_color =[0, 0, 1, 1], pos_hint={'bottom':1, 'center_x':1})
self.goback.bind(on_press=self.go_back)
self.add_widget(self.goback)
def go_back(self, instance):
MovieScript.screen_manager.current = "Main Page"
另一种方法是使用 kivy 语言。例如,您可以为 SearchMovie
class:
kv = '''
<SearchMovie>:
cols: 1
ScrollView:
size_hint: 1, 0.9
GridLayout:
id: grid
cols: 1
size_hint_y: None
height: self.minimum_height
Button:
size_hint: 1, 0.1
text: "Go Back"
background_color: [0, 0, 1, 1]
pos_hint: {'bottom': 1, 'center_x': 1}
on_press: root.go_back()
'''
那么你的实际SearchMovie
class可以是:
class SearchMovie(GridLayout):
global MovieList
def __init__(self, **kwargs):
super().__init__(**kwargs)
subgrid = self.ids.grid
movlist = MovieList[0]["Box1"]
nmovlist = []
for i in movlist:
nmovlist.append(str(i))
nmovlist.sort()
for i in nmovlist:
movlab = Label(text=i, size_hint_y=None, height=40)
subgrid.add_widget(movlab)
def go_back(self, instance):
MovieScript.screen_manager.current = "Main Page"
请务必在创建 SearchMovie
实例之前加载上述 kv
字符串。