删除动作栏图标Kivy

2024-09-28 03:12:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有我的动作栏在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的文档中找不到任何东西…有人有什么想法吗?在


Tags: 代码posapptopwithcoloriconbackground
3条回答

请阅读代码中的注释,以便Frankenstein您首选的隐藏ActionPrevious各个方面的方法;删除它似乎不是一个选项,DropDown当我在Kivy v1.10.0上尝试时,{没有乐趣,但是可以隐藏部分或全部内容。在

#!/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()

似乎无法禁用图标。
参考:Kivy sources@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_height:https://kivy.org/docs/api-kivy.uix.actionbar.html#kivy.uix.actionbar.ActionPrevious.app_icon_height

这是一种黑客攻击,但由于kivy.uix.actionbar.ActionPrevious实际上是BoxLayout的子类,因此您可以在创建后使用remove_widget()或{}等方法来操作其内容:

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()

相关问题 更多 >

    热门问题