大约两个星期以来,我一直在学习 GTK +3.0。我想做一个好的桌面应用程序,但是越来越觉得GTK,比较神秘,我一直是为web开发的,6个多月的android开发,我有一年Windows Presentation Foundation的经验,从来没有发现这么难做任何事情,比如 GTK,它现在正试图在应用程序中实现拖动文件,最后我找到了一个示例,虽然它的工作原理并不完全理解对某些代码行的需求。

所需的解释是关于方法 Gtk.Window.drag_dest_set , Window.on_drag_motionWindow.on_drag_drop
为什么需要使用Gdk.drag_status ?
为什么我需要做 widget.drag_get_data(context, context.list_targets()[-1], time) ?

from gi.repository import Gtk, Gdk 
 
class Window(Gtk.Window): 
    ''' 
    Main Window 
    ''' 
    def __init__(self): 
        super(Window, self).__init__(title=TITLE) 
 
        self.connect('delete-event', Gtk.main_quit) 
 
        ''' 
        set up drag 
        ''' 
        self.connect('drag-motion', self.on_drag_motion) 
        self.connect('drag-drop', self.on_drag_drop) 
        self.connect('drag-data-received', self.on_drag_data_received) 
        self.drag_dest_set(0, [], 0) 
 
        self.show() 
        Gtk.main() 
 
    def on_drag_motion(self, widgt, context, c, y, time): 
        Gdk.drag_status(context, Gdk.DragAction.COPY, time) 
        return True 
 
    def on_drag_drop(self, widget, context, x, y, time): 
        widget.drag_get_data(context, context.list_targets()[-1], time) 
 
    def on_drag_data_received(self, widget, drag_context, x, y, data, info, time): 
        files = data.get_text().rstrip('\n').split('\n') 
 
        for file in files: 
            print(file) 
 
if __name__ == '__main__': 
    Window() 

请您参考如下方法:

也许自上一个版本以来发生了一些变化:http://python-gtk-3-tutorial.readthedocs.org/en/latest/drag_and_drop.html

一开始有一个关于在特定情况下使用某些方法的小说明。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!