python-3.x - 如何让 ScrollView 在 python 中重置到屏幕顶部

标签 python-3.x kivy kivy-language

我在我的应用程序中重复使用屏幕。我第一次返回上一个屏幕时,滚动将重置到顶部,但在第三次或之后的任何时间都不会。

我正在设计一个应用程序,用户可以通过单击屏幕一上的按钮从选项列表中进行选择。根据他们单击哪个,屏幕 2 上将显示不同的信息列表。我需要在python中添加按钮和标签,因为按钮的信息和数量是动态的。我不知道如何将滚动重置到屏幕顶部。我在按下按钮后运行的函数中添加了一行 ScrollView.scroll_y=1 。这在第一次通过时有效,但在此之后的任何时候都无效。因此,如果您运行代码两次,屏幕将重置到顶部。但是如果你第三次通过它,它将不再从屏幕顶部开始。谁能告诉我是什么导致了这种行为,或者有没有人有更好的方法来使用 python 语言在按下按钮时重置屏幕?

这是我的 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()

和 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

最佳答案

您的专线 ScrollView.scroll_y=1不做任何事情,因为它在类 ScrollView 上设置一个值而不是您在显示器中显示的任何实例。

尝试添加以下行:self.ids.scrollview.scroll_y = 1在您的 add_buttons 末尾方法(您可以在 add_labels 方法的末尾添加相同的行)。

关于python-3.x - 如何让 ScrollView 在 python 中重置到屏幕顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54601948/

相关文章:

python - Kivy 中的动态网格,每个网格元素包含多个小部件

python - RecycleView 内的 Kivy 尺寸标签

python - 将可变长度元组列表转换为字典

android - 如何在 Kivy + Python 中读取 NFC 标签?

android - 为 android 商店签署 kivy 应用程序时出错

python - 如何防止小部件重叠?

Python/Kivy : Call function from one class to another class and show widget in Python

python - 二进制模式下 io.FileIO 和 open() 的区别

python - Pandas 在 Column 的特定行范围内搜索最大值

python - 不和谐.py |发出静音命令,无法获取成员名称