小部件(按钮显示)被困在UI的左下角(Python与Kivy)

时间:2018-03-16 02:26:53

标签: python kivy

问题:

如何让Widget(按钮显示)不会卡在UI的左下角?

目标:

我希望WidgetwithButton中的按钮与SomeScreen中的按钮格式相匹配。相反,它被卡在左下角,几乎看不见。

代码如下。

Python代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

class ScreenManagement(ScreenManager):
    pass

class AnotherScreen(Screen):
    pass

class MainScreen(Screen):
    pass

class WidgetwithButton(Widget):
    pass

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

class MainApp(App):
    def build(self):
        return presentation

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

KV代码:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:

<WidgetwithButton>:
    Button:
        text: "stuff"
        font_size: 30
        size_hint: 0.25, 0.1
        pos_hint: {"x":0, "top": 0.69}
<MainScreen>:
    WidgetwithButton:    
    FloatLayout:    
        Button:
            text: "stuff"
            font_size: 30
            size_hint: 0.25, 0.1
            pos_hint: {"x":0, "top": 0.8}

输出:

Output:

注意:

左下角&#39;东西&#39;理想情况下应该与上面的按钮大小相同,略低于它(如pos_hint代码所示)

1 个答案:

答案 0 :(得分:1)

WidgetWithButtonWidget的子类,因此它不会对size_hintpos_hint执行任何操作(这适用于布局,每个布局使用的方式略有不同),如果你想要相同的行为,你可能想让它继承FloatLayout。或者,您可以使用possize代替pos_hintsize_hint

<WidgetwithButton>:
    Button:
        text: "stuff"
        font_size: 30
        # size_hint: 0.25, 0.1
        # the following line will emulate that size_hint without the need of a layout
        size: root.width * 0.25, root.height * 0.1

        # pos_hint: {"x":0, "top": 0.69}
        # the following two lines will emulate the same effect without the need of a layout
        x: root.x
        top: self.height and root.y + root.height * 0.69