【问题标题】:Why doesn't the Kivy ActionBar Work Properly?为什么 Kivy ActionBar 不能正常工作?
【发布时间】:2020-07-13 10:43:20
【问题描述】:

我正在尝试使用 kivy pingball 和操作栏示例的想法创建一个操作栏。
这是我的代码:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window

Builder.load_string("""
<TitleBar>:
    ActionBar:
        pos_hint: {'top':1}
        ActionView:
            use_separator: True
            ActionPrevious:
                title: 'Action Bar'
                with_previous: False
            ActionOverflow:
            ActionButton:
                icon: 'atlas://data/images/defaulttheme/audio-volume-high'
            ActionButton:
                important: True
                text: 'Important'
            ActionButton:
                text: 'Btn2'
            ActionButton:
                text: 'Btn3'
            ActionButton:
                text: 'Btn4'
            ActionGroup:
                text: 'Group1'
                ActionButton:
                    text: 'Btn5'
                ActionButton:
                    text: 'Btn6'
                ActionButton:
                    text: 'Btn7'
""")
class TitleBar(Widget):
    pass
class TrialApp(App):

    def build(self):
        Window.size=(875,575)
        Window.borderless=True
        return TitleBar()

TrialApp().run()

尽管使用了pos_hint: {'top':1},但为什么ActionBar 会到底部?
Python 3.7.5 Windows 10

【问题讨论】:

    标签: python python-3.x error-handling kivy


    【解决方案1】:

    您的App 的根Widget 是一个简单的Widget,它不打算用作容器,并且不支持pos_hint 之类的东西。尝试改变:

    class TitleBar(Widget):
    

    class TitleBar(FloatLayout):
    

    这会使您的根Widget 成为FloatLayout,它将处理pos_hint

    【讨论】:

    • 谢谢 bt 我在 30 秒前找到了 :) 并发布了答案。
    • 如何让动作按钮执行动作?
    • ActionButton 只是Button 的扩展,因此您可以像普通的Button 一样指定'on_press' 或on_release
    • 它没有'on_moveover'或'on_hover'之类的吗?
    • 我不这么认为。 KivyMD 有一个HoverBehavior,您可能可以使用它。你可能会想出自己的悬停使用类似Window.bind(mouse_pos=self.on_motion) 的东西并使用collide_point() 来检查鼠标位置是否在感兴趣的Widget 上。
    【解决方案2】:

    当您阅读 kivy.uix.actionbar 的文档时,您会得到它而不是 Widget,您需要使用 FloatLayout
    所以代码一定是:

    from kivy.uix.floatlayout import FloatLayout
    class TitleBar(FloatLayout):
        pass
    

    【讨论】:

      猜你喜欢
      • 2016-07-16
      • 2019-01-04
      • 2020-09-03
      • 2016-10-10
      • 2016-10-24
      • 2017-02-27
      • 2017-07-08
      • 2014-11-23
      • 2021-03-07
      相关资源
      最近更新 更多