问题:
如何让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}
输出:
注意:
左下角&#39;东西&#39;理想情况下应该与上面的按钮大小相同,略低于它(如pos_hint
代码所示)
答案 0 :(得分:1)
WidgetWithButton
是Widget
的子类,因此它不会对size_hint
和pos_hint
执行任何操作(这适用于布局,每个布局使用的方式略有不同),如果你想要相同的行为,你可能想让它继承FloatLayout
。或者,您可以使用pos
和size
代替pos_hint
和size_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