我在scroll label
中找到FloatLayout
时遇到问题。
原始代码来自Alexander Taylor。链接:https://github.com/kivy/kivy/wiki/Scrollable-Label
我想让scroll label
定位如图所示。
位置值为:
vtop =屏幕高度/ 10
vbottom =屏幕高度/ 20
hleft =屏幕宽度/ 20
hright =屏幕宽度/ 40
我在原始代码中添加了一个FloatLayout。这是我改变的代码。
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
long_text = 'yay moo cow foo bar moo baa ' * 200
Builder.load_string('''
<ScrollableLabel>:
FloatLayout:
Label:
size: root.width - root.width/20 - root.width/40, root.height - root.height/10 - root.height/20
pos: root.width /20, root.height / 20
#size_hint: (None, None)
# valign: 'top'
size_hint_y: None
height: self.texture_size[1]
text_size: self.width - self.width/20 - self.width/40, self.height - self.height/10 - self.height/20
# text_size: self.width , None
font_size: 30
text: root.text
''')
class ScrollableLabel(ScrollView):
text = StringProperty('')
class ScrollApp(App):
def build(self):
return ScrollableLabel(text=long_text)
if __name__ == "__main__":
ScrollApp().run()
我已经尝试了一些方法来定位滚动标签,正如您在上面的代码中看到的那样,但它没有给我所需的结果。
我会问你的想法,使代码正确。谢谢!
答案 0 :(得分:2)
我认为您的问题是您的应用程序的主要小部件是从ScrollView派生的,而您只希望内部区域可滚动。您在ScrollView中有FloatLayout。我建议使用主布局小部件并将ScrollableLabel嵌入其中。
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty
from kivy.lang import Builder
long_text = 'yay moo cow foo bar moo baa ' * 100
Builder.load_string('''
<MainApp>:
ScrollableLabel:
id: scrollable
pos: self.parent.pos[0]+self.parent.size[0]/20, self.parent.pos[1]+self.parent.size[1]/20
size_hint: 18.5 / 20.0, 0.85
<ScrollableLabel>:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
text: root.text
''')
class MainApp(FloatLayout):
pass
class ScrollableLabel(ScrollView):
text = StringProperty('')
class ScrollApp(App):
def build(self):
app = MainApp()
app.ids["scrollable"].text = long_text
return app
if __name__ == "__main__":
ScrollApp().run()