<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python &#187; Uncategorized</title>
	<atom:link href="/python/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>https://talkera.org/python</link>
	<description>Programming Blog</description>
	<lastBuildDate>Sun, 01 Feb 2015 17:15:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1</generator>
	<item>
		<title>Creating a webbrowser with Python and PyQT (tutorial)</title>
		<link>https://talkera.org/python/creating-a-webbrowser-with-python-and-pyqt-tutorial/</link>
		<comments>https://talkera.org/python/creating-a-webbrowser-with-python-and-pyqt-tutorial/#comments</comments>
		<pubDate>Fri, 30 Jan 2015 20:40:16 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[pyqt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[webbrowser]]></category>

		<guid isPermaLink="false">https://talkera.org/python/?p=115</guid>
		<description><![CDATA[In this tutorial we will build a webbrowser with Python. We will use PyQT.  Install the required packages: [crayon-54cf0d42a4841193253420/] If you have not done our first tutorial, you could try it. If python-kde4 cannot be found update your repository to find it. If you are on Ubuntu or Debian Linux use this link. Creating the [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>In this tutorial we will build a webbrowser with Python. We will use PyQT.  Install the required packages:</p><pre class="crayon-plain-tag">sudo pip install python-qt4
sudo apt-get install qt4-designer
sudo apt-get install pyqt4-dev-tools
sudo apt-get install python-kde4</pre><p>If you have not done our <a href="/python/building-an-application-gui-with-pyqt-beginners-tutorial/" target="_blank">first tutorial</a>, you could try it. If python-kde4 cannot be found update your repository to find it. If you are on Ubuntu or Debian Linux use <a href="http://packages.ubuntu.com/precise/i386/python-kde4/download" target="_blank">this link</a>.</p>
<p><strong>Creating the UI with PyQT</strong><br />
Start qt4-designer from your applications menu. The QT Designer application will appear:</p>
<div id="attachment_77" style="width: 590px" class="wp-caption alignnone"><a href="/python/wp-content/uploads/2015/01/QT_Designer.jpg"><img class="wp-image-77 size-large" src="/python/wp-content/uploads/2015/01/QT_Designer-1024x557.jpg" alt="QT_Designer" width="580" height="315" /></a><p class="wp-caption-text">QT_Designer</p></div>
<p>Select Main Window and press Create. We now have our designer window open.  Drag a<em> KWebView</em> component on the window. If you have a<em> QtWebView</em> in the component list. use that instead. We also add an<em> Line Edit</em> on top. Press File &gt; Save As &gt; browser.ui.  Run the command:</p><pre class="crayon-plain-tag">pyuic4 browser.ui &gt; browser.py</pre><p>This will generate a Python file. Remove the line &#8220;from kwebview import KWebView&#8221; from the bottom of the browser.py file. Change KWebView to QtWebView. We want to use QtWebView instead. If you are lazy to change that, take the browser.py file from below.</p>
<p><strong>Creating the logic.</strong><br />
Create a file called<strong><em> r</em></strong><em><strong>un.py</strong></em> with this contents:</p><pre class="crayon-plain-tag">import sys
from browser import BrowserDialog
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class MyBrowser(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        QWebView.__init__(self)
        self.ui = BrowserDialog()
        self.ui.setupUi(self)
        self.ui.lineEdit.returnPressed.connect(self.loadURL)
 
    def loadURL(self):
        url = self.ui.lineEdit.text()
        self.ui.qwebview.load(QUrl(url))
        self.show()  
        #self.ui.lineEdit.setText("")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyBrowser()
    myapp.ui.qwebview.load(QUrl('https://talkera.org/python'))
    myapp.show()
    sys.exit(app.exec_())</pre><p>This code will use the UI as defined in browser.py and add logic to it. The lines</p><pre class="crayon-plain-tag">self.ui.lineEdit.returnPressed.connect(self.loadURL)
 
    def loadURL(self):
        url = self.ui.lineEdit.text()
        self.ui.qwebview.load(QUrl(url))
        self.show()  
        #self.ui.lineEdit.setText("")</pre><p>The first line defines the callback or event. If a person presses enter (returnPressed), it will call the function loadURL. It makes sure that once you press enter, the page is loaded with that function. If you did everything correctly, you should be able to run the browser with the command:</p><pre class="crayon-plain-tag">python run.py</pre><p>Please make sure you type the full url, e.g.  : https://talkera.org including the http:// part.  Your browser should now start:</p>
<p><a href="/python/wp-content/uploads/2015/01/browser.png"><img class="alignnone wp-image-119 size-large" src="/python/wp-content/uploads/2015/01/browser-1024x742.png" alt="browser" width="580" height="420" /></a></p>
<p>If your code does not run, please use the codes below (or look at the differences and change whats wrong):</p>
<p><strong>browser.py</strong></p><pre class="crayon-plain-tag"># -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'browser.ui'
#
# Created: Fri Jan 30 20:49:32 2015
#      by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!

import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class BrowserDialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(1024, 768)
        self.qwebview = QWebView(Dialog)
        self.qwebview.setGeometry(QtCore.QRect(0, 50, 1020, 711))
        self.qwebview.setObjectName(_fromUtf8("kwebview"))
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(10, 20, 1000, 25))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Browser", "Browser", None))</pre><p><strong>run.py</strong></p><pre class="crayon-plain-tag">import sys
from browser import BrowserDialog
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class MyBrowser(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        QWebView.__init__(self)
        self.ui = BrowserDialog()
        self.ui.setupUi(self)
        self.ui.lineEdit.returnPressed.connect(self.loadURL)
 
    def loadURL(self):
        url = self.ui.lineEdit.text()
        self.ui.qwebview.load(QUrl(url))
        self.show()  
        #self.ui.lineEdit.setText("")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyBrowser()
    myapp.ui.qwebview.load(QUrl('https://talkera.org/python'))
    myapp.show()
    sys.exit(app.exec_())</pre><p></p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fcreating-a-webbrowser-with-python-and-pyqt-tutorial%2F&amp;linkname=Creating%20a%20webbrowser%20with%20Python%20and%20PyQT%20%28tutorial%29" title="Facebook" rel="nofollow" target="_blank"></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fcreating-a-webbrowser-with-python-and-pyqt-tutorial%2F&amp;linkname=Creating%20a%20webbrowser%20with%20Python%20and%20PyQT%20%28tutorial%29" title="Twitter" rel="nofollow" target="_blank"></a><a class="a2a_button_google_plus" href="http://www.addtoany.com/add_to/google_plus?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fcreating-a-webbrowser-with-python-and-pyqt-tutorial%2F&amp;linkname=Creating%20a%20webbrowser%20with%20Python%20and%20PyQT%20%28tutorial%29" title="Google+" rel="nofollow" target="_blank"></a><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fcreating-a-webbrowser-with-python-and-pyqt-tutorial%2F&amp;linkname=Creating%20a%20webbrowser%20with%20Python%20and%20PyQT%20%28tutorial%29" title="Reddit" rel="nofollow" target="_blank"></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fcreating-a-webbrowser-with-python-and-pyqt-tutorial%2F&amp;linkname=Creating%20a%20webbrowser%20with%20Python%20and%20PyQT%20%28tutorial%29" title="StumbleUpon" rel="nofollow" target="_blank"></a><a class="a2a_button_pinterest" href="http://www.addtoany.com/add_to/pinterest?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fcreating-a-webbrowser-with-python-and-pyqt-tutorial%2F&amp;linkname=Creating%20a%20webbrowser%20with%20Python%20and%20PyQT%20%28tutorial%29" title="Pinterest" rel="nofollow" target="_blank"></a><a class="a2a_dd a2a_target addtoany_share_save" href="https://www.addtoany.com/share_save#url=http%3A%2F%2Ftalkera.org%2Fpython%2Fcreating-a-webbrowser-with-python-and-pyqt-tutorial%2F&amp;title=Creating%20a%20webbrowser%20with%20Python%20and%20PyQT%20%28tutorial%29" id="wpa2a_2"></a></p>]]></content:encoded>
			<wfw:commentRss>https://talkera.org/python/creating-a-webbrowser-with-python-and-pyqt-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
