【问题标题】:Python character encoding with dnd data.get_text()使用 dnd data.get_text() 进行 Python 字符编码
【发布时间】:2014-05-18 16:49:46
【问题描述】:

我想使用 gtk 拖放来获取拖放文件的名称。这是代码:

[...]
    self.scrolledwindow_play.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)
    self.scrolledwindow_play.connect('drag_data_received', self.on_drag_data_received_to_play)
    self.scrolledwindow_play.drag_dest_set_target_list(None)
    self.scrolledwindow_play.drag_dest_add_text_targets()

def on_drag_data_received_to_play(self, widget, drag_context, x,y, data,info, time):
    print data.get_text()

当我将文件拖放到小部件上时,我会:

file:///home/mnrl/Masa%C3%BCst%C3%BC/spinbutton.py 像这样。但真正的文件路径是:

file:///home/mnrl/Masaüstü/spinbutton.py

为什么编码字符不同?如何正确获取文件路径?

【问题讨论】:

    标签: python drag-and-drop pygtk


    【解决方案1】:

    像这样在 python 文件的头部定义你的编码:

    http://legacy.python.org/dev/peps/pep-0263/

    确保您的编辑器也设置为相同的编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-22
      • 2011-09-21
      • 2017-04-01
      • 2015-12-17
      • 2015-11-29
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多