[PyQt Example] basic web browser

Post here if you need help with creating a Graphical User Interface in Python.

[PyQt Example] basic web browser

Postby metulburr » Sat Feb 09, 2013 6:19 pm

Code: Select all
import sys
from PyQt4 import QtCore, QtGui, QtWebKit

class Browser(QtGui.QMainWindow):

   def __init__(self):
      QtGui.QMainWindow.__init__(self)
      self.resize(800,600)
      self.centralwidget = QtGui.QWidget(self)

      self.mainLayout = QtGui.QHBoxLayout(self.centralwidget)
      self.mainLayout.setSpacing(0)
      self.mainLayout.setMargin(1)

      self.frame = QtGui.QFrame(self.centralwidget)

      self.gridLayout = QtGui.QVBoxLayout(self.frame)
      self.gridLayout.setMargin(0)
      self.gridLayout.setSpacing(0)

      self.horizontalLayout = QtGui.QHBoxLayout()
      self.tb_url = QtGui.QLineEdit(self.frame)
      self.bt_back = QtGui.QPushButton(self.frame)
      self.bt_ahead = QtGui.QPushButton(self.frame)

      self.bt_back.setIcon(QtGui.QIcon().fromTheme("go-previous"))
      self.bt_ahead.setIcon(QtGui.QIcon().fromTheme("go-next"))

      self.horizontalLayout.addWidget(self.bt_back)
      self.horizontalLayout.addWidget(self.bt_ahead)
      self.horizontalLayout.addWidget(self.tb_url)
      self.gridLayout.addLayout(self.horizontalLayout)

      self.html = QtWebKit.QWebView()
      self.gridLayout.addWidget(self.html)
      self.mainLayout.addWidget(self.frame)
      self.setCentralWidget(self.centralwidget)

      self.connect(self.tb_url, QtCore.SIGNAL("returnPressed()"), self.browse)
      self.connect(self.bt_back, QtCore.SIGNAL("clicked()"), self.html.back)
      self.connect(self.bt_ahead, QtCore.SIGNAL("clicked()"), self.html.forward)

      self.default_url = "http://www.google.com/"
      self.tb_url.setText(self.default_url)
      self.setWindowTitle('Python Browser')
      self.browse()

   def browse(self):
      url = self.tb_url.text()
      if self.tb_url.text():
         pass
      else:
          self.default_url
      self.html.load(QtCore.QUrl(url))
      self.html.show()

if __name__ == "__main__":

   app = QtGui.QApplication(sys.argv)
   main = Browser()
   main.show()
   sys.exit(app.exec_())
Last edited by Yoriz on Tue Mar 19, 2013 1:02 pm, edited 1 time in total.
Reason: Added Example to the title
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1449
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests