使用 python 在 nautilus 扩展中使用 gtk

发布于 2024-09-10 22:16:42 字数 1647 浏览 2 评论 0原文

以下代码

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()时,消息会正确显示)

The following code

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)

does not produce any Messages.
However

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)

works nicely and produces the requested Messages.

Can someone explain this - or better - provide a workaround?

(Even better is that when moving the alert()-call into get_file_items() the message appears correctly)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在我安 2024-09-17 22:16:42

nautilus 邮件列表上,Ahmad Sherif 发现了我的错误:

gtk.MessageDialog 无法使用您的代码,因为第五个参数
必须是 string 或 None,并且变量名称的类型
nautilus.FileInfo,这意味着您需要调用 alert(name.get_name())
不仅仅是alert(name)
请参阅文档 有关 nautilus.FileInfo 的更多信息。

感谢艾哈迈德指出了这一点。

第二篇文章中,Ahmad 解释了如何更好地调试:

我想如果你启动了 Nautilus,你就可以获得这样的调试信息
从终端。 […]
您应该先退出 Nautilus (nautilus -q),然后启动它 (nautilus --no-desktop)。
如果[鹦鹉螺]确实[自动]重新生成[退出后],您应该编辑
/usr/share/applications/nautilus.desktop 如下(它的备份将
是个好主意):

  • 将“X-GNOME-AutoRestart=true”替换为“X-GNOME-AutoRestart=false”
  • 在行首用“#”注释掉“X-GNOME-Autostart-Phase=Desktop”。实际上我忘记了为什么这样做,但这就是我的配置:)。
  • 添加此行“AutostartCondition=GNOME /apps/nautilus/preferences/show_desktop”
  • 最后,您应该重新启动会话,然后尝试退出并再次启动。

积分转到wrc1944 […]以获得重生解决方案。< /p>

应用艾哈迈德解释的步骤,我能够看到错误代码生成的错误消息。

On the nautilus mailing list Ahmad Sherif found my error:

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 the docs for more info about nautilus.FileInfo.

Thanks to Ahmad for pointing this out.

In a second posting Ahmad explained how to debug better:

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 wrc1944 […] for the respawning solution.

Applying the steps Ahmad explained I was able to see the error-message my faulty code generated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文