【问题标题】:Am I using Gio GFile monitor_file wrong?我使用 Gio GFile monitor_file 错了吗?
【发布时间】:2014-01-19 05:12:14
【问题描述】:

我正在像这样使用 Gio monitor_file。

def callback(*args):
    print 'ok'

gio_pointer = Gio.File.new_for_path(os.environ['HOME']+'/temp')
monitor = gio_pointer.monitor_file(Gio.FileMonitorFlags.NONE, None)
monitor.connect("changed", callback)

修改后的文件没有任何反应。 Gio 适用于创建、读取和写入等其他文件操作。

是我用错了,还是系统问题?

我的环境:Gtk 图形、Python、Linux Ubuntu 12.10、普通电脑。

【问题讨论】:

    标签: python linux file-management gio


    【解决方案1】:

    它可能会失败,因为信号工作需要 gobject 的主循环。

    以下完整示例对我有用:

    import os
    from gi.repository import Gtk, Gio
    
    # This allows Ctrl+C to exit the program
    import signal
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    
    def callback(m, f, o, event):
        # Without this check, multiple 'ok's will be printed for each file change
        if event == Gio.FileMonitorEvent.CHANGES_DONE_HINT:
            print ('ok')
    
    gio_file = Gio.File.new_for_path(os.environ['HOME']+'/temp')
    monitor = gio_file.monitor_file(Gio.FileMonitorFlags.NONE, None)
    monitor.connect("changed", callback)
    
    Gtk.main()
    

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      相关资源
      最近更新 更多