I am reusing screens in my app. The first time I go back to a previous screen, the scroll will reset to the top, but it will not during the third time, or any time after that.
I am designing an app that will have a user choose from a list of options by clicking the button on screen one. Depending on which one they click, a different list of information will show up on screen two. I need to add the buttons and labels in python because the information and number of buttons will be dynamic. I can't figure out how to reset the scroll to the top of the screen. I added a line ScrollView.scroll_y=1 into the function that runs after pressing a button. This works the first time through, but not any time after that. So if you run through the code twice, the screen will reset to the top. But if you go through it a third time, it will no longer start at the top of the screen. Can anyone tell me what is causing this behavior, or does anyone have a better way to reset the screen on a button press using the python language?
Here is my main.py:
import kivy
kivy.require('1.10.1')
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.lang.builder import Builder
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
class MainScreen(Screen):
pass
class FirstScreen(Screen):
container=ObjectProperty(None)
def add_buttons(self):
self.ButtonList=[1,2,3,4,5,6,7,8,9,10]
for i in range(0,10):
self.ButtonList[i]=Button(text='Button {}'.format(i), id=str(i), size_hint=(1,None), on_press=self.switchscreens)
self.container.add_widget(self.ButtonList[i])
def switchscreens(self,instance):
self.container.clear_widgets()
self.manager.current='secondscreen'
class SecondScreen(Screen):
container=ObjectProperty(None)
def add_labels(self):
self.LabelList=[1,2,3,4,5,6,7,8,9,10]
for i in range(0,10):
self.LabelList[i]=Label(text='Label {}'.format(i), id=str(i), size_hint=(1,None))
self.container.add_widget(self.LabelList[i])
self.SwitchBackButton=Button(text='Main Screen', id='switchbutton', size_hint=(1,None), height=30, on_press=self.switchback)
self.container.add_widget(self.SwitchBackButton)
def switchback(self,instance):
ScrollView.scroll_y=1
self.container.clear_widgets()
self.manager.current='main'
class ScreenManagement(ScreenManager):
pass
presentation=Builder.load_file("Switch.kv")
class SwitchApp(App):
def build(self):
return presentation
SwitchApp().run()
And Switch.kv:
ScreenManagement:
name:'screen_manager'
id:screenmanager
MainScreen:
FirstScreen:
on_pre_enter:
self.add_buttons()
SecondScreen:
on_pre_enter:
self.add_labels()
<MainScreen>:
id:main_screen
name: 'main'
ScrollView:
id:scrollview
name:'scrollview'
GridLayout:
cols:1
padding:10
spacing:10
size_hint: None, None
width:800
height: self.minimum_height
Label:
text: 'Main Menu'
Button:
text: 'First Screen'
size_hint: 1,None
on_release: app.root.current= 'firstscreen'
<FirstScreen>:
id:first_screen
name: 'firstscreen'
container:container
ScrollView:
id:scrollview
name:'scrollview'
GridLayout:
id:container
cols:1
padding:10
spacing:10
size_hint: None, None
width:800
height: self.minimum_height
<SecondScreen>:
id:second_screen
name: 'secondscreen'
container:container
ScrollView:
id:scrollview
name:'scrollview'
GridLayout:
id:container
cols:1
padding:10
spacing:10
size_hint: None, None
width:800
height: self.minimum_height
Your line ScrollView.scroll_y=1
doesn't do anything since it is setting a value on the class ScrollView
instead of any instance that your are showing in your display.
Try adding the line: self.ids.scrollview.scroll_y = 1
at the end of your add_buttons
method (and you can add the same line at the end of your add_labels
method).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With