PyQt实现WebView 发表于 2016-09-08 | 分类于 技术 | 阅读次数 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384#!/usr/bin/env python# __*__ coding:utf-8 __*__import sys,timefrom PyQt4.Qt import *from PyQt4.QtWebKit import *from PyQt4.QtCore import *from PyQt4 import QtGuiimport ConfigParserclass WebView(QWebView): def __init__(self): super(WebView,self).__init__() #读取配置文件,配置文件管理访问的url及窗口是否全屏 cf = ConfigParser.ConfigParser() cf.read('WeChat.conf') url = cf.get('WeChat','url') showFullScreen_enable = cf.get('WeChat','showFullScreen_enable') #打开url self.load(QUrl(url)) self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks) self.page().linkClicked.connect(self.linkClicked) #设置窗口标题、图标 self.setWindowTitle(u'二维码扫码') self.setWindowIcon(QtGui.QIcon('WeChat.png')) #创建托盘 self.createTrayIcon() #托盘点击事件 self.trayIcon.activated.connect(self.iconActivated) if showFullScreen_enable == 'True': self.showFullScreen() else: pass self.show() def linkClicked(self, url): self.load(QUrl(url)) def closeEvent(self, event): QtGui.QMessageBox.about(self,u'提示 ',u'不允许关闭! ') event.ignore() def focusOutEvent(self, QFocusEvent): self.trayIcon.showMessage("警告", "窗口已失焦", 2) def focusInEvent(self, QFocusEvent): self.trayIcon.showMessage("提示", "窗口已聚焦", 1) def createTrayIcon(self): icon = QtGui.QIcon('WeChat.png') self.trayIcon = QtGui.QSystemTrayIcon(self) self.trayIcon.setIcon(icon) self.trayIcon.setToolTip(self.tr("二维码扫码")) self.trayIcon.setVisible(True) def iconActivated(self, reason): if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick): self.showFullScreen() elif reason == QtGui.QSystemTrayIcon.MiddleClick: self.showFullScreen()if __name__ == '__main__': app = QApplication(sys.argv) #解决打包exe后,页面中文乱码问题 app.Encoding(QApplication.UnicodeUTF8) utfcodec = QTextCodec.codecForName("UTF-8") QTextCodec.setCodecForTr(utfcodec) QTextCodec.setCodecForLocale(utfcodec) QTextCodec.setCodecForCStrings(utfcodec) webView = WebView() sys.exit(app.exec_())