python-3.x - ScrollView 不允许我滚动
问题描述
我一直在开发我的第一个 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"
任何帮助将不胜感激!
解决方案
您必须设置height
. subgrid
默认值为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
班级制定规则:
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 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"
请确保在创建实例kv
之前加载上述字符串。SearchMovie
推荐阅读
- java - Java中的对象输入和输出
- python - 有没有办法在 Python 中显示 filecmp.dircmp 的进度?
- java - 使用 Dagger 注入接口的所有实现
- ubuntu - Certbot 使用 XAMPP 在 Ubuntu 16.04 上自动生成证书
- amazon-web-services - 如何删除亚马逊 ECR 中的旧图像
- css - css的循环动画
- android - 如何使用 NavigationView 在工具栏中添加另一个图标
- bash - 在bash中按字典顺序比较两个包含'_'的字符串
- javascript - 如何在 Angular 9 项目中安装 request-promise
- java - 如何从字节数组创建输出文件,以便在打开输出文件时,显示的数据与字节数组中的数据相同?