使用python在nautilus扩展中使用gtk

以下代码

import gtk
import nautilus
import os
def alert(message):
    """A function to debug"""
    dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, message)
    dialog.run()
    dialog.destroy()

class TestExtension(nautilus.MenuProvider):
    def __init__(self):
        pass

    def get_file_items(self, window, files):
        items = []
        """Called when the user selects a file in Nautilus."""
        item = nautilus.MenuItem("NautilusPython::test_item", "Test", "Test")
        item.connect("activate", self.menu_activate_cb, files)
        items.append(item)
        return items

    def menu_activate_cb(self, menu, files):
        """Called when the user selects the menu."""
        for name in files:
            alert(name)

不会产生任何消息.
然而

import easygui
import nautilus
import os

def alert(message):
    """A function to debug"""
    easygui.msgbox(message)

class TestExtension(nautilus.MenuProvider):
    def __init__(self):
        pass

    def get_file_items(self, window, files):
        items = []
        """Called when the user selects a file in Nautilus."""
        item = nautilus.MenuItem("NautilusPython::test_item", "Test", "Test")
        item.connect("activate", self.menu_activate_cb, files)
        items.append(item)
        return items

    def menu_activate_cb(self, menu, files):
        """Called when the user selects the menu."""
        for name in files:
            alert(name)

很好地工作,并产生所请求的消息.

有人可以解释这个 – 或更好 – 提供一个解决方法吗?

(更好的是,当移动alert() – 调用get_file_items()时,消息正确显示)

最佳答案 在
nautilus mailing list艾哈迈德谢里夫​​发现我的错误:

gtk.MessageDialog is not working with your code because the fifth argument
has to be either string or None, and the variable name is of type
nautilus.FileInfo, which means you need to call alert(name.get_name())
not just alert(name)
Please refer to 07001 for more info about nautilus.FileInfo.

感谢艾哈迈德指出这一点.

second posting Ahmad中解释了如何更好地调试:

I think you could’ve obtained such debug info if you launched Nautilus
from terminal. […]
You should quit Nautilus first (nautilus -q) then launch it (nautilus --no-desktop).
If [nautilus] did [automatically] re-spawn [after quitting], you should edit
/usr/share/applications/nautilus.desktop as follows (A backup of it would
be a good idea):

  • Replace “X-GNOME-AutoRestart=true” with “X-GNOME-AutoRestart=false”
  • Comment this line out “X-GNOME-Autostart-Phase=Desktop” by “#” at the beginning of the line. Actually I forgot why I did this but this how my is my configuration :).
  • Add this line “AutostartCondition=GNOME /apps/nautilus/preferences/show_desktop”
  • Finally, you should restart your session, then try quitting and launching again.

Credits go to 07003 […] for the respawning solution.

应用Ahmad解释的步骤我能够看到错误消息我生成的错误代码.

点赞