【问题标题】:Add buttons to ActionBar app on KIVY. Python将按钮添加到 KIVY 上的 ActionBar 应用程序。 Python
【发布时间】:2015-01-01 08:05:05
【问题描述】:

这是我从软件附带的 KIVY 示例目录中复制的代码,我正在尝试修改它,并添加其他小部件。

.KV 文件

#:kivy 1.0

<ActionBar>:
    height: '48dp'
    size_hint_y: None
    spacing: '4dp'
    canvas:
        Color:
            rgba: self.background_color
        BorderImage:
            border: root.border
            pos: self.pos
            size: self.size
            source: self.background_image

<ActionView>:
    orientation: 'horizontal'
    canvas:
        Color:
            rgba: self.background_color
        BorderImage:
            pos: self.pos
            size: self.size
            source: self.background_image

<ActionSeparator>:
    size_hint_x: None
    minimum_width: '2sp'
    width: self.minimum_width
    canvas:
        Rectangle:
            pos: self.x, self.y + sp(4)
            size: self.width, self.height - sp(8)
            source: self.background_image

<ActionButton,ActionToggleButton>:
    background_normal: 'atlas://data/images/defaulttheme/' + ('action_bar' if self.inside_group else 'action_item')
    background_down: 'atlas://data/images/defaulttheme/action_item_down'
    size_hint_x: None if not root.inside_group else 1
    width: [dp(48) if (root.icon and not root.inside_group) else max(dp(48), (self.texture_size[0] + dp(32))), self.size_hint_x][0]
    color: self.color[:3] + [0 if (root.icon and not root.inside_group) else 1]

    Image:
        opacity: 1 if (root.icon and not root.inside_group) else 0
        source: root.icon
        mipmap: root.mipmap
        pos: root.x + dp(4), root.y + dp(4)
        size: root.width - dp(8), root.height - sp(8)

<ActionGroup>:
    size_hint_x: None
    width: self.texture_size[0] + dp(32)

<ActionCheck>:
    background_normal: 'atlas://data/images/defaulttheme/action_bar' if self.inside_group else 'atlas://data/images/defaulttheme/action_item'

<ActionPrevious>:
    size_hint_x: 1
    minimum_width: '100sp'
    important: True
    BoxLayout:
        orientation: 'horizontal'
        pos: root.pos
        size: root.size
        Image:
            source: root.previous_image
            opacity: 1 if root.with_previous else 0
            allow_stretch: True
            size_hint_x: None
            width: self.texture_size[0] if root.with_previous else dp(8)
            mipmap: root.mipmap
        Image:
            source: root.app_icon
            allow_stretch: True
            size_hint_x: None
            width: min(self.height, self.texture_size[0]) if self.texture else self.height
            mipmap: root.mipmap
        Widget:
            size_hint_x: None
            width: '5sp'
        Label:
            text: root.title
            text_size: self.size
            color: root.color
            shorten: True
            halign: 'left'
            valign: 'middle'

<ActionGroup>:
    background_normal: 'atlas://data/images/defaulttheme/action_group'
    background_down: 'atlas://data/images/defaulttheme/action_group_down'
    background_disabled_normal: 'atlas://data/images/defaulttheme/action_group_disabled'
    border: 30, 30, 3, 3
    ActionSeparator:
        pos: root.pos
        size: root.separator_width, root.height
        opacity: 1 if root.use_separator else 0
        background_image: root.separator_image if root.use_separator else 'action_view'

<ActionOverflow>:
    border: 3, 3, 3, 3
    background_normal: 'atlas://data/images/defaulttheme/action_item'
    background_down: 'atlas://data/images/defaulttheme/action_item_down'
    background_disabled_normal: 'atlas://data/images/defaulttheme/button_disabled'
    size_hint_x: None
    minimum_width: '48sp'
    width: self.texture_size[0] if self.texture else self.minimum_width
    canvas.after:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: root.center_x - sp(16), root.center_y - sp(16)
            size: sp(32), sp(32)
            source: root.overflow_image

<ActionDropDown>:
    auto_width: False

<ContextualActionView>:

.PY 文件

from kivy.base import runTouchApp
from kivy.lang import Builder

runTouchApp(Builder.load_string('''
ActionBar:
    pos_hint: {'top':1}
    ActionView:
        use_separator: True
        ActionPrevious:
            title: 'Action Bar'
            with_previous: False
        ActionOverflow:
        ActionButton:
            text: 'Btn0'
            icon: 'atlas://data/images/defaulttheme/audio-volume-high'
        ActionButton:
            text: 'Btn1'
        ActionButton:
            text: 'Btn2'
        ActionButton:
            text: 'Btn3'
        ActionButton:
            text: 'Btn4'
        ActionGroup:
            text: 'Group1'
            ActionButton:
                text: 'Btn5'
            ActionButton:
                text: 'Btn6'
            ActionButton:
                text: 'Btn7'
'''))

我试图向这个应用程序添加滚动视图功能,但我不断收到错误消息。有人可以帮我添加一个按钮作为示例来帮助我完成这段代码吗?

【问题讨论】:

  • 请发布您的错误信息,以便其他人帮助确定问题所在。

标签: python kivy


【解决方案1】:

你的问题是你不明白它是如何工作的。

仅当您创建 kivy.app.App 的子类并使其名称以“App”结尾时才会加载 .kv 文件。然后加载一个没有“App”的同名 .kv 文件。将 Builder.load_string 中的所有内容移至 .kv 文件并创建 App 的子类即可避免混淆。

现在您可以像这样将 ActionBar 和新 Button 放在水平 BoxLayout 中:

ActionBarTest.kv:

BoxLayout:
    orientation: "horizontal"
    ActionBar: 
        pos_hint: {'top':1} 
        ActionView: 
            use_separator: True 
            ...
    Button:
        #new Button
        text: "Hello World"

main.py

import kivy
from kivy.app import App

class ActionBarTestApp(App):
    def build(self):
        #self.root is already defined, because
        #you set a root object in .kv file
        return self.root

app = ActionBarTestApp()
app.run()

【讨论】:

  • 非常感谢!非常感谢这一点。我和你的指南修改了
猜你喜欢
  • 2017-10-21
  • 2014-03-18
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多