开发者

Customizing context menus in python wwebkit gtk programs

I've been on working on using the python webkit and gtk modules to turn a HTML / Javascript page into a desktop application. To do this, I've created a webkit window with hardly any features other than the webview.

import webkit, gtk, subprocess
w = gtk.Window(gtk.WINDOW_TOPLEVEL)
w.set_resizable(False)
w.set_size_request(900,600)
w.connect("delete_event", gtk.main_quit)
scroll_window=gtk.ScrolledWindow(None, None)
web = webkit.WebView()
web.open('/home/user/HTML/mypage.html')
settings = web.get_settings()
settings.set_property('enable-default-context-menu', True)
scroll_window.add(web)
w.add(scroll_window)
w.show_all()
gtk.main()

This works fine, apart from the contex开发者_高级运维t menus. When I click right on most areas of the page, the context menu gives me the following options: back, forward, stop, reload.

But when I right-click on a link, I get: open link, open link in new window, download linked file, copy link location.

I would like to customize this so that when I right-click on a link I get only: open link

I've googled and looked at other posts on stack overflow, but although I can find out how to disable the context menus, I cannot find out how to customize them.

P.S. Unless you can't tell, I'm quite new to python and very new to gtk and webkit modules.


For customizing the context menu, you first need to add the corresponding 'context-menu' callback. This function can modify the displayed context menu using append or remove methods. You can append a gtk.ImageMenuItem. This should work as an example:

def callback(webview, context_menu, hit_result_event, event):
    option = gtk.ImageMenuItem('Do it')
    option.connect('activate', option_activate_cb)
    context_menu.append(option)
    option.show()

def option_activate_cb(image_menu_item):
    print('It works.')

web.connect('context-menu', callback)

One additional note: You do not need to enable the context menu. It is enabled by default.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜