【问题标题】:Widget (Button Display) Stuck in Bottom Left Corner on UI (Python with Kivy)小部件(按钮显示)卡在 UI 的左下角(带有 Kivy 的 Python)
【发布时间】:2018-03-16 18:05:13
【问题描述】:

问题:

如何防止小部件(按钮显示)卡在 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}

输出:

注意:

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

【问题讨论】:

  • 我认为您假设 minimal reproducible example 指的是您的项目,实际上 minimal reproducible example 指的是允许您重现特定问题的通用代码,这应该通过消除分散注意力的不必要元素,例如新类型、变量等。您的问题并不普遍,它取决于您设计的结构。
  • 我已更新问题以显示手头的基本问题。
  • 我什么都不懂。
  • 我用最少的可运行代码再次更新了问题。

标签: python kivy


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多