移除Kivy中的操作栏图标

7
我正在使用上述代码在Kivy中创建我的操作栏。

ActionBar:
  background_color:0,191,255,0.5
  pos_hint: {'top':1}
  
  ActionView:
   ActionPrevious:
    with_previous: False
    text:"    [b]Dhobiwala.com[/b]"
    
    app_icon:""
                ### I make app_icon as a blank string...but it takes default icon...I totally want to remove icon from my action bar

    markup:True
    font_size:"16dp"
    on_release:root.Show_Devlopers_Info()
    

根据上述代码...我想从状态栏中删除图标...我在Kivy文档中甚至找不到任何内容...有人有什么想法吗?

可能是在Python Kivy应用程序中使用Action Bar时遇到问题的重复问题。 - Peter Badida
5个回答

4

看起来禁用图标是不可能的。
参考:Kivy在github上的源代码(https://github.com/kivy/kivy/blob/856b305c0c876e53e802f1ac9ae16c21fa91ac1e/kivy/uix/actionbar.py#L214)。
相关部分:

if not self.app_icon:
        self.app_icon = 'data/logo/kivy-icon-32.png'

你可以尝试使用一张完全透明的小图片作为图标来规避此问题。
另外,你可以尝试将图标的大小减小到0。查看属性app_icon_widthapp_icon_heighthttps://kivy.org/docs/api-kivy.uix.actionbar.html#kivy.uix.actionbar.ActionPrevious.app_icon_height

但我也想把那个空格去掉...图标不会像JAVA制作的Android应用程序一样自动调整大小...所以这会造成麻烦...如果有一种方法可以删除图标和它占用的空间...那对我来说就很好了...谢谢。 - Nisarg
如果它可以调整大小,那就太好了。因为有时图标会重叠在其他操作栏小部件上...有没有解决方案可以根据不同的分辨率进行设置,因为这是一个Android应用程序。 - Nisarg
我编辑了答案,包括有关app_icon_height / app_icon_width的信息。我不确定它是否有效,但尝试一下也无妨 :) - leovp
你可以通过给我点赞来提高我的声望,感谢。 - Nisarg
设置 app_icon_width: 1app_icon_height: 1 是有效的。如果将其设置为0,则无效。 - Halil İbrahim Oymacı

3
请阅读代码中的注释,以便混合您首选的隐藏各种 ActionPrevious 方式; 删除它似乎不是一个选项,并且当我在 Kivy v1.10.0 上尝试使用 DropDown 时,没有成功,但可以隐藏部分或整个视图。
#!/usr/bin/env python

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout

## Un-comment if you wish to add to the action view via Python
# from kivy.uix.actionbar import ActionButton, ActionGroup

kv = """
<SomeLayout_GridLayout>:
    cols: 1
    rows: 2
    row_force_default: True
    rows_minimum: {0: ActionBar.height, 1: self.height - ActionBar.height}
    SomeMenu_ActionBar:
        id: ActionBar

<SomeMenu_ActionBar@ActionBar>:
    ActionView:
        id: ActionView
        ## Choose one of the following three pre-coded options
        HiddenIcon_ActionPrevious:
        # HiddenText_ActionPrevious:
        # Hidden_ActionPrevious:

        ## Do the ActionGroup(s) with ActionButton(s) within thing
        ActionGroup:
            id: App_ActionGroup
            mode: 'spinner'
            text: 'App'
            ActionButton:
                text: 'Settings'
                on_press: app.open_settings()
            ActionButton:
                text: 'Quit'
                on_press: app.get_running_app().stop()

        ActionGroup:
            id: File_ActionGroup
            mode: 'spinner'
            text: 'File'
            ActionButton:
                text: 'Open'
            ActionButton:
                text: 'Save'

## Inspired by: https://stackoverflow.com/a/36201399/2632107
##  Hide just the icon, but keep the text, note though
##  that one will lose the 'on_press' and similar methods
<HiddenIcon_ActionPrevious@ActionPrevious>:
    title: app.title if app.title is not None else 'Action Previous'
    with_previous: False
    app_icon: ''
    app_icon_width: 0
    app_icon_height: 0
    ## Comment out the following two lines if you wish to have
    ##  ActionGroup(s) and or ActionButtion(s) pushed to the right
    size_hint_x: None
    width: len(self.title) * 10

## Keep the icon and UI methods but hide the text
<HiddenText_ActionPrevious@ActionPrevious>: #
    with_previous: False
    on_press: print(self)
    title: ''

## Hide everything
<Hidden_ActionPrevious@ActionPrevious>:
    with_previous: False
    on_press: print(self) ## method that will not be called easily
    title: '' ## Try placing text here, only a few pixels should show
    size_hint: None, None
    size: 0, 0
"""


class SomeLayout_GridLayout(GridLayout):
    pass


class SomeApp(App):
    def build(self):
        ## Cannot set this in '__init__' for some reason
        self.title = 'Some Sweet App'

        Builder.load_string(kv)

        some_layout = SomeLayout_GridLayout()
        ## Uncomment next line if ya wish to use 'add_widget'
        ##  method on ActionView and add ActionGroup(s) and/or
        ##  ActionButton(s) via Python
        # some_actionview = some_layout.ids.ActionBar.ids.ActionView
        return some_layout

if __name__ == '__main__':
    SomeApp().run()

2

这是一个技巧,但由于kivy.uix.actionbar.ActionPrevious实际上是BoxLayout的子类,因此您可以在创建后使用remove_widget()clear_widgets()等方法来操作其内容:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

kv = """
<MyWidget>:
    ap: ap
    ActionBar:
        background_color:0,191,255,0.5
        pos_hint: {'top':1}

        ActionView:
            ActionPrevious:
                id: ap
                with_previous: False
                text:"    [b]Dhobiwala.com[/b]"

                markup:True
                font_size:"16dp"
"""

Builder.load_string(kv)

class MyWidget(BoxLayout):
    def __init__(self, *args):
        super(MyWidget, self).__init__(*args)
        self.ap.clear_widgets()

class MyButtonsApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyButtonsApp().run()

如果您觉得这个问题很有趣,可以通过点击+1来增加我的声望,因为这个问题对其他人也可能有帮助。谢谢。 - Nisarg
文本框中没有显示ActionPrevious的内容,只是空白。 - jacobay43

1

Kivi 2.0.0

要在 ActionBar 中轻松删除徽标,请将其 ActionPrevious 子项设置为空字符串。

ActionPrevious:
    title: 'Title'
    with_previous: False
    app_icon: '' 

运行此脚本作为示例

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

runTouchApp(Builder.load_string('''
ActionBar:
    pos_hint: { 'top': 1 }
    background_color: [0, 1, .76, 1]
    background_image: ''
    ActionView:    
        ActionPrevious:
            title: 'Title'
            with_previous: False
            app_icon: ''    # Set the Icon to an empty String           
        ActionOverflow:
        ActionButton:
            text: 'Settings'
            on_press: app.open_settings()
        ActionButton:
            text: 'Quit'
            on_press: app.get_running_app().stop()
'''))

根据Kivy文档,app_icon不过是一个StringProperty

-1
ActionPrevious:
   title: 'Recently Available'
   with_previous: False
   app_icon: '' if self.app_icon is not None else "assets/images/P1.png"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接