我想在一个屏幕内和同一屏幕上添加一个滚动视图。我想有一个重定向到主屏幕的按钮。下面显示的代码似乎无效。这是我的python代码(假设我已经导入了相关模块):
class Main(Screen):
pass
class View(Screen):
view = ObjectProperty(Non
def __init__(self, **kwargs):
super(View, self).__init__(**kwargs)
Clock.schedule_once(self.create_scrollview)
def create_scrollview(self, dt):
base = ["element {}".format(i) for i in range(40)]
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
layout.bind(minimum_height=layout.setter("height"))
for element in base:
layout.add_widget(Button(text=element, size=(50, 50), size_hint=(1, None),background_color=(0.5, 0.5, 0.5, 1), color=(1, 1, 1, 1)))
scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
scrollview.add_widget(layout)
self.view.add_widget(scrollview)
class Manager(ScreenManager): #transition of windows
def __init__(self, **kwargs):
super(Manager, self).__init__(**kwargs)
pass
kv = Builder.load_file('main.kv')
class NotesApp(App):
def build(self):
return kv
if __name__ == '__main__':
NotesApp().run()
这是我的kv文件:
Manager:
Main:
View:
<Button>:
font_size:35
<Label>:
font_size:45
<Main>:
name:'main'
<View>:
name: 'view'
FloatLayout:
Button:
pos_hint:{'x':0.2,'top':0.975}
size_hint:0.6,0.15
text: 'Back'
on_release:
app.root.current = 'main'
root.manager.transition.direction = 'right'
答案 0 :(得分:0)
id: view
添加到FloatLayout:
view: view
添加<View>:
。这是将ObjecProperty连接到ID。id: btn_view
添加到Button:
<View>:
name: 'view'
view: view
FloatLayout:
id: view
Button:
id: btn_view
pos_hint: {'x':0.2, 'top':0.975}
size_hint: 0.6, 0.15
text: 'Back'
on_release:
root.manager.transition.direction = 'right'
root.manager.current = 'main'
scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height - self.ids.btn_view.height),
bar_width=8, bar_color=[1, 0, 0, 1], bar_inactive_color=[0, 0, 1, 1],
effect_cls="ScrollEffect", scroll_type=['bars'])