我正在try 创建一个简单的菜单分隔符,但我很难做到这一点.我在So上和它的documentation页上搜索过,我发现它超级令人困惑和无用,我甚至试图阅读Menu课:

class Menu(object):
    """A description of a menu.

    A menu description is immutable.

    It is created with a sequence of :class:`Menu.Item` instances, or a single
    callable which must return a generator for the menu items.

    First, non-visible menu items are removed from the list, then any instances
    of :attr:`SEPARATOR` occurring at the head or tail of the item list are
    removed, and any consecutive separators are reduced to one.
    """
    #: A representation of a simple separator
    SEPARATOR = MenuItem('- - - -', None)

    def __init__(self, *items):
        self._items = tuple(items)

在其中我找到了下面的表示,我使用了这样的表示:

sep = pystray.MenuItem("- - - -", None)

但它没有创建分隔符,而是创建了一个文本为- - - -的菜单项

你可以在下面找到一个最小的可重现的例子:

import pystray
from PIL import Image
def item1_action(icon, item):
    print("Item 1 clicked")
def item2_action(icon, item):
    print("Item 2 clicked")
def quit_action(icon, item):
    print("Quit clicked")

item1 = pystray.MenuItem("Item 1", item1_action)
item2 = pystray.MenuItem("Item 2", item2_action)
sep = pystray.MenuItem("- - - -", None)
quit_item = pystray.MenuItem("Quit", quit_action)
menu = (item1, item2,s, quit_item)
image = Image.open('icon.png')
icon = pystray.Icon("test", image, "test", menu)
icon.run()

推荐答案

您已经正确地确定了指示使用分隔符的点.但是,您应该特别使用SEPARATOR属性,而不是它的赋值,因为有人可能实际上想要创建一个带有四个连字符的菜单项,而pystray不适合违背他们的意愿自动将其转换为分隔符.

话虽如此,但您需要替换以下内容:

sep = pystray.MenuItem("- - - -", None)

有了这一点:

sep = pystray.Menu.SEPARATOR

Python相关问答推荐

绘制系列时如何反转轴?

inspect_asm不给出输出

在Windows上启动新Python项目的正确步骤顺序

分组数据并删除重复数据

如何使用scipy从频谱图中回归多个高斯峰?

max_of_three使用First_select、second_select、

Gekko:Spring-Mass系统的参数识别

处理(潜在)不断增长的任务队列的并行/并行方法

有症状地 destruct 了Python中的regex?

在Pandas DataFrame操作中用链接替换'方法的更有效方法

pandas滚动和窗口中有效观察的最大数量

Pandas Loc Select 到NaN和值列表

在Python中,从给定范围内的数组中提取索引组列表的更有效方法

python panda ExcelWriter切换动态公式到数组公式

将链中的矩阵乘法应用于多组值

无法在Spyder上的Pandas中将本地CSV转换为数据帧

判断Python操作:如何从字面上得到所有decorator ?

提取数组每行的非零元素

如何使用matplotlib查看并列直方图

我可以不带视频系统的pygame,只用于游戏手柄输入吗?''