AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 30514
Accepted
8128
8128
Asked: 2011-03-16 10:19:55 +0800 CST2011-03-16 10:19:55 +0800 CST 2011-03-16 10:19:55 +0800 CST

为什么我的消息菜单代码在拆分为功能时不起作用?

  • 772

下面是两个python程序。它们完全一样,除了一个被分成两个功能。然而,只有一个被分成两个功能的功能不起作用——第二个功能不起作用。为什么会这样?

请注意,代码取自这篇有用的博客文章。

没有功能(作品):

import gtk

def show_window_function(x, y):
    print x
    print y

# get the indicate module, which does all the work
import indicate

# Create a server item
mm = indicate.indicate_server_ref_default()
# If someone clicks your server item in the MM, fire the server-display signal
mm.connect("server-display", show_window_function)
# Set the type of messages that your item uses. It's not at all clear which types
# you're allowed to use, here.
mm.set_type("message.im")
# You must specify a .desktop file: this is where the MM gets the name of your
# app from.
mm.set_desktop_file("/usr/share/applications/nautilus.desktop")
# Show the item in the MM.
mm.show()

# Create a source item
mm_source = indicate.Indicator()
# Again, it's not clear which subtypes you are allowed to use here.
mm_source.set_property("subtype", "im")
# "Sender" is the text that appears in the source item in the MM
mm_source.set_property("sender", "Unread")
# If someone clicks this source item in the MM, fire the user-display signal
mm_source.connect("user-display", show_window_function)
# Light up the messaging menu so that people know something has changed
mm_source.set_property("draw-attention", "true")
# Set the count of messages in this source.
mm_source.set_property("count", "15")
# If you prefer, you can set the time of the last message from this source,
# rather than the count. (You can't set both.) This means that instead of a
# message count, the MM will show "2m" or similar for the time since this
# message arrived.
# mm_source.set_property_time("time", time.time())
mm_source.show()

gtk.mainloop()

使用函数(执行第二个函数但实际上不起作用):

import gtk

def show_window_function(x, y):
    print x
    print y

# get the indicate module, which does all the work
import indicate

def function1():
    # Create a server item
    mm = indicate.indicate_server_ref_default()
    # If someone clicks your server item in the MM, fire the server-display signal
    mm.connect("server-display", show_window_function)
    # Set the type of messages that your item uses. It's not at all clear which types
    # you're allowed to use, here.
    mm.set_type("message.im")
    # You must specify a .desktop file: this is where the MM gets the name of your
    # app from.
    mm.set_desktop_file("/usr/share/applications/nautilus.desktop")
    # Show the item in the MM.
    mm.show()

def function2():
    # Create a source item
    mm_source = indicate.Indicator()
    # Again, it's not clear which subtypes you are allowed to use here.
    mm_source.set_property("subtype", "im")
    # "Sender" is the text that appears in the source item in the MM
    mm_source.set_property("sender", "Unread")
    # If someone clicks this source item in the MM, fire the user-display signal
    mm_source.connect("user-display", show_window_function)
    # Light up the messaging menu so that people know something has changed
    mm_source.set_property("draw-attention", "true")
    # Set the count of messages in this source.
    mm_source.set_property("count", "15")
    # If you prefer, you can set the time of the last message from this source,
    # rather than the count. (You can't set both.) This means that instead of a
    # message count, the MM will show "2m" or similar for the time since this
    # message arrived.
    # mm_source.set_property_time("time", time.time())
    mm_source.show()

function1()
function2()
gtk.mainloop()
indicator
  • 1 1 个回答
  • 347 Views

1 个回答

  • Voted
  1. Best Answer
    Alistair Buxton
    2011-03-16T11:28:17+08:002011-03-16T11:28:17+08:00

    mm_source 对 function2 是本地的。当 function2 完成时,它会超出范围并被垃圾收集。这会导致它被快速添加,然后在您看到它之前从菜单中删除。

    要阻止这种情况发生,只需将 mm_source 对象返回给调用代码,并将其保存在变量中。您可能也想对您的 mm 做同样的事情。如下:

    
    import gtk
    
    def show_window_function(x, y):
        print x
        print y
    
    # get the indicate module, which does all the work
    import indicate
    
    def function1():
        # Create a server item
        mm = indicate.indicate_server_ref_default()
        # If someone clicks your server item in the MM, fire the server-display signal
        mm.connect("server-display", show_window_function)
        # Set the type of messages that your item uses. It's not at all clear which types
        # you're allowed to use, here.
        mm.set_type("message.im")
        # You must specify a .desktop file: this is where the MM gets the name of your
        # app from.
        mm.set_desktop_file("/usr/share/applications/nautilus.desktop")
        # Show the item in the MM.
        mm.show()
        return mm
    
    def function2():
        # Create a source item
        mm_source = indicate.Indicator()
        # Again, it's not clear which subtypes you are allowed to use here.
        mm_source.set_property("subtype", "im")
        # "Sender" is the text that appears in the source item in the MM
        mm_source.set_property("sender", "Unread")
        # If someone clicks this source item in the MM, fire the user-display signal
        mm_source.connect("user-display", show_window_function)
        # Light up the messaging menu so that people know something has changed
        mm_source.set_property("draw-attention", "true")
        # Set the count of messages in this source.
        mm_source.set_property("count", "15")
        # If you prefer, you can set the time of the last message from this source,
        # rather than the count. (You can't set both.) This means that instead of a
        # message count, the MM will show "2m" or similar for the time since this
        # message arrived.
        # mm_source.set_property_time("time", time.time())
        mm_source.show()
        return mm_source
    
    my_mm = function1()
    my_mm_source = function2()
    gtk.mainloop()
    
    
    • 2

相关问题

  • 如何更改/自定义通知区域中的图标?

  • 为什么我的时钟、指示器小程序和通知区域有时会在我重新启动时移动?我怎样才能防止这种情况?

  • 如何在我的指标小程序中获取 Gmail 通知(不让 Evolution 保持打开状态)?

  • 指标小程序:如何摆脱时钟?

  • 删除在线状态菜单,但保留注销菜单?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    我需要什么命令来解压缩/提取 .tar.gz 文件?

    • 8 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Martin Hope
    EmmyS 我需要什么命令来解压缩/提取 .tar.gz 文件? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve