Почему код обмена сообщениями не работает при разделении на функции?

Возможно, это может сработать?

#!/bin/sh
/usr/sbin/archttp64 81

Поместите скрипт в, например, /usr/local/bin/MyScript.sh

Если у вас есть версия для настольного ПК, просто нажмите на Shutdown icon в верхнем правом углу и добавьте скрипт в startup applications. Если нет, взгляните на эту нить

4
задан 11 October 2011 в 00:02

2 ответа

mm_source является локальным для функции2. Когда функция 2 завершает работу, она выходит из области видимости и собирает мусор. Это приведет к быстрому добавлению и удалению из меню, прежде чем вы сможете его увидеть.

Чтобы остановить это, просто верните объект mm_source в вызывающий код и сохраните его в переменной. Вероятно, вы захотите сделать то же самое с вашим миллиметром. Как показано ниже:


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
ответ дан 25 May 2018 в 22:32
  • 1
    Я думаю, вы абсолютно правы. Можете ли вы перейти к подходу ООП сделать это проще? – 8128 16 March 2011 в 00:01
  • 2
    Конечно. Разумеется, это более структурированный способ связывания объектов, чем просто использование случайных глобальных переменных. Вам все равно придется делать то же самое, но в рамках вашего класса, а не с глобальными. – Alistair Buxton 16 March 2011 в 00:55

mm_source является локальным для функции2. Когда функция 2 завершает работу, она выходит из области видимости и собирает мусор. Это заставляет его быстро добавлять и удалять из меню, прежде чем вы сможете его увидеть.

Чтобы остановить это, просто верните объект mm_source в вызывающий код и сохраните его в переменной. Вероятно, вы захотите сделать то же самое с вашим миллиметром. Как показано ниже:


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
ответ дан 2 August 2018 в 03:48

Другие вопросы по тегам:

Похожие вопросы: