PyQt实现WebView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# __*__ coding:utf-8 __*__
import sys,time
from PyQt4.Qt import *
from PyQt4.QtWebKit import *
from PyQt4.QtCore import *
from PyQt4 import QtGui
import ConfigParser
class 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_())