bob dylan
bob dylan

Reputation: 1109

Add possibility to launch commands from browser

I have a simple browser in python

#!/usr/bin/env python
import sys
import gtk
import webkit
DEFAULT_URL = 'http://www.stackoverflow.com' # Change this as you Wish
class SimpleBrowser: # needs GTK, Python, Webkit-GTK
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        self.window.connect('delete_event', self.close_application)
        self.window.set_default_size(350, 20)
        vbox = gtk.VBox(spacing=5)
        vbox.set_border_width(5)
        self.txt_url = gtk.Entry()
        self.txt_url.connect('activate', self._txt_url_activate)
        self.scrolled_window = gtk.ScrolledWindow()
        self.webview = webkit.WebView()
        self.scrolled_window.add(self.webview)
        vbox.pack_start(self.scrolled_window, fill=True, expand=True)
        self.window.add(vbox)
    def _txt_url_activate(self, entry):
        self._load(entry.get_text())
    def _load(self, url):
        self.webview.open(url)
    def open(self, url):
        self.txt_url.set_text(url)
        self.window.set_title('%s' % url)
        self._load(url)
    def show(self):
        self.window.show_all()
    def close_application(self, widget, event, data=None):
        gtk.main_quit()
if __name__ == '__main__':
    if len(sys.argv) > 1:
        url = sys.argv[1]
    else:
        url = DEFAULT_URL
    gtk.gdk.threads_init()
    browser = SimpleBrowser()
    browser.open(url)
    browser.show()
    gtk.main()

I would like my browser to have the possibility to click on an image to change the wallpaper on the current system. (I just need an example, i don't need portability just now, just execute a command in a shell when onclick is enough.)

Upvotes: 3

Views: 1332

Answers (1)

outoftime
outoftime

Reputation: 765

Install PySide on ubuntu

# apt-get install python-pyside

Or find how to install it into your platform

import PySide.QtWebKit
import sys
from PyQt4 import QtGui


class BrowserWindow(PySide.QtWebKit.QWebView):

    SCRIPT_TEMPLATE = 'document.elementFromPoint({}, {});'

    def __init__(self, _parent):
        super(BrowserWindow, self).__init__()
        PySide.QtWebKit.QWebView(None)
        print('init')

    def mousePressEvent(self, event):
        # prepare script to execute
        frame = self.page().mainFrame()
        scroll = frame.scrollPosition()
        x = scroll.x() + event.x()
        y = scroll.y() + event.y()
        script = self.SCRIPT_TEMPLATE.format(x, y)

        # get 'src' attribute
        obj = frame.evaluateJavaScript(script)
        if 'src' in obj:
            src = obj['src']
        else:
            src = None

        # delegate event handling to super class
        if not src:
            return super(BrowserWindow, self).mousePressEvent(event)

        # download image and set wallpaper


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    view = BrowserWindow(None)
    view.load("http://google.com")
    view.show()
    sys.exit(app.exec_())

You need to find out how to download image by url and set it as wallpaper for your OS. Both are trivial.

Upvotes: 3

Related Questions