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

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

下面是两个 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()

答案1

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

相关内容