简体   繁体   English

Scrollview向一侧填充比另一侧多(Python Kivy)

[英]Scrollview Padding One Side More than the Opposite Side (Python Kivy)

Is there a way in the kv language to pad one side of a scrollview widget more than its opposite? 在kv语言中,有没有一种方法可以使scrollview小部件的一侧比另一侧更多?

Here's a runnable example... 这是一个可运行的示例...

Python Code: Python代码:

from kivy.app import App
# kivy.require("1.10.0")
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty, ObjectProperty, NumericProperty

class ScrollableLabel(ScrollView):
    text = "blah blah blah"

class AnotherScreen(Screen):
    pass

class BackHomeWidget(Widget):
    pass

class MainScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("Test_Running_Console.kv")

class MainApp(App):

    def build(self):
        return presentation

if __name__ == "__main__":
    MainApp().run()

Kv Code: 验证码:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<SmallNavButton@Button>:    
    font_size: 32
    size: 125, 50    
    color: 0,1,0,1

<MedButton@Button>:
    font_size: 30
    size_hint: 0.25, 0.1
    color: 0,1,0,1

<BackHomeWidget>:
    SmallNavButton:
        on_release: app.root.current = "main"
        text: "Home"
        pos: root.x, root.top - self.height

<ScrollableLabel>:
    Label:
        id: dataentryinstructions
        text: root.text
        font_size: 20
        text_size: self.width, None
        size_hint_y: None
        height: self.texture_size[1]
        padding_y: 10
        padding_x: 200

<MainScreen>:
    name: "main"
    FloatLayout: 
        MedButton:
            on_release: app.root.current = "newgarage"
            text: "Create New"
            pos_hint: {"x":0.3728, "top": 0.4}

<AnotherScreen>:
    name: "newgarage"
    ScrollableLabel:
    BackHomeWidget:
    FloatLayout:
        MedButton
            text: "1. Stuff"
            pos_hint: {"x":0, "top": 0.75}

As one may guess from the look of it, this pads the left and right side at 200 and the top and bottom at 10. But what if I want to pad the left side at 200 and the right side at 120? 从外观上可以猜到,它在200左右填充左侧和右侧,在10左右填充顶部,但是如果我想在200左侧填充左侧,在120右侧填充右侧呢?

In this particular case, I just want the scrollview to take up the right half of the screen. 在这种情况下,我只希望滚动视图占据屏幕的右半部分。 (to avoid overlap with buttons) (避免与按钮重叠)

Forgive me if I overlooked this in the Kivy documentation. 如果我在Kivy文档中忽略了这一点,请原谅我。 From the documentation, it looked like Gridlayout & BoxLayout can accept four arguments for padding (one for each direction), but wasn't sure how to incorporate that into a scrollview use case. 从文档看来,Gridlayout&BoxLayout可以接受四个用于填充的参数(每个方向一个),但是不确定如何将其合并到scrollview用例中。

You could put the whole label in a BoxLayout and add empty widgets with the desired width. 您可以将整个标签放在BoxLayout中,然后添加具有所需宽度的空窗口小部件。

 <ScrollableLabel>:
    Widget:
        size_hint_x: None
        width: 200
    ScrollView:
        Label:
            id: dataentryinstructions
            text: root.text
            font_size: 20
            text_size: self.width, None
            size_hint_y: None
            height: self.texture_size[1]
            padding_y: 10
    Widget:
        size_hint_x: None
        width: 120

This is the whole code: 这是整个代码:

from kivy.app import App
# kivy.require("1.10.0")
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty, ObjectProperty, NumericProperty

class ScrollableLabel(BoxLayout):
    text = "blah blah blah"

class AnotherScreen(Screen):
    pass

class BackHomeWidget(Widget):
    pass

class MainScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_string("""
#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<SmallNavButton@Button>:    
    font_size: 32
    size: 125, 50    
    color: 0,1,0,1

<MedButton@Button>:
    font_size: 30
    size_hint: 0.25, 0.1
    color: 0,1,0,1

<BackHomeWidget>:
    SmallNavButton:
        on_release: app.root.current = "main"
        text: "Home"
        pos: root.x, root.top - self.height

<ScrollableLabel>:
    Widget:
        size_hint_x: None
        width: 200
    ScrollView:
        Label:
            id: dataentryinstructions
            text: root.text
            font_size: 20
            text_size: self.width, None
            size_hint_y: None
            height: self.texture_size[1]
            padding_y: 10
    Widget:
        size_hint_x: None
        width: 120


<MainScreen>:
    name: "main"
    FloatLayout: 
        MedButton:
            on_release: app.root.current = "newgarage"
            text: "Create New"
            pos_hint: {"x":0.3728, "top": 0.4}

<AnotherScreen>:
    name: "newgarage"
    ScrollableLabel:
    BackHomeWidget:
    FloatLayout:
        MedButton
            text: "1. Stuff"
            pos_hint: {"x":0, "top": 0.75}
""")

class MainApp(App):

    def build(self):
        return presentation

if __name__ == "__main__":
    MainApp().run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM