gpt4 book ai didi

python - 如何在 Kivy ScrollView 中自动向下滚动?

转载 作者:行者123 更新时间:2023-11-30 22:54:54 25 4
gpt4 key购买 nike

我将小部件添加到 ScrollView 中的 GridLayout,以便其内容动态扩展。
默认情况下,如果用户不滚动,无论您添加多少个小部件, View 都会保持在顶部。如果用户滚动, View 就会附加到这一点,但对于我来说,必须向下滚动(即使是一点点) View 才能始终显示最新内容似乎有点烦人。如何让它默认显示最下面的部分?

这里是示例代码,以防万一:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout

Builder.load_string('''
<MessageView>:
canvas:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size

<Message>:
canvas:
Color:
rgba: 0, 1, 0, 0.3
Rectangle:
pos: self.pos
size: self.size
''')

class Message(Widget):
pass

class MessageView(ScrollView):
pass

class TestApp(App):
def msg_in(self, btn):
msg = Message()
msg.size_hint = [None, None]
self.msg_layout.add_widget(msg)

def build(self):
self.scr = Screen()
self.sv1_main = MessageView(pos_hint={"top": 0.87, "center_x": 0.5},
size_hint=(0.97, 0.65))
self.msg_layout = GridLayout(cols=1,
size_hint_y=None)
self.msg_layout.bind(minimum_height=self.msg_layout.setter('height'))
self.bt1_main = Button(size_hint=(0.1, 0.078),
pos_hint={"top": 0.097, "center_x": 0.927},
on_press=self.msg_in)
self.scr.add_widget(self.sv1_main)
self.sv1_main.add_widget(self.msg_layout)
self.scr.add_widget(self.bt1_main)
return self.scr

TestApp().run()

最佳答案

您可以使用scroll_to添加新内容后的方法。

class TestApp(App):
def msg_in(self, btn):
msg = Message()
msg.size_hint = [None, None]
self.msg_layout.add_widget(msg)
self.sv1_main.scroll_to(msg)
# ...

关于python - 如何在 Kivy ScrollView 中自动向下滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37619893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com