gpt4 book ai didi

python - Kivy:GridLayout 内的水平滚动标签

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:54 25 4
gpt4 key购买 nike

我正在尝试改编 ScrollView doc 中的代码在 GridLayout 内的水平轴上获得可滚动标签。 GridLayout 不应该可滚动,而只能滚动其中的标签。

对于我的应用程序,我无法使用 kv 语言(无论是在 .kv 还是构建器中),因此代码必须使用 python。

from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.app import runTouchApp

layout = GridLayout(cols=1, spacing=10)

for i in range(10):
label = Label(text="abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz", height = 30, size_hint_x=None, width=400)
label.bind(minimum_width=label.setter('width'))
root = ScrollView(size_hint=(None, 1), size=(label.width, label.height))
root.add_widget(label)
layout.add_widget(root)


runTouchApp(layout)

最佳答案

到目前为止,这是有效的:

import kivy

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView

class Test(App):
def build(self):
layout = GridLayout (cols=3)
for i in range(3):
l = Label(font_size=15, size_hint_x= None, width=300, text="This is a very looooooooooooooooooooonnnnnnnnnnnnnnnnnnnng text. Indeed it is a very loooooooooooooooooooooonnnnnnnnnnnnnnnng text")
l.bind(size_hint_min_x=l.setter('width'))
scroll = ScrollView(size_hint=(None, None), size=(100, 400), pos_hint={'center_x':.5, 'center_y':.5})
scroll.add_widget(l)

layout.add_widget(scroll)
return layout


Test().run()

关于python - Kivy:GridLayout 内的水平滚动标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608890/

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