Pyqt 触发一个事件,打开外部链接,我找到了这个方法,供大家参考
1. QDesktopServices 的openUrl 方法
QtGui.QDesktopServices.openUrl(QtCore.QUrl('http://www.hao123.com'))
2.Python 自带的webbrowser 浏览器控制模块
webbrowser提供了三种方法
import webbrowser
webbrowser.open(url, new=0, autoraise=True)
webbrowser.open_new(url)
webbrowser.open_new_tab(url)
3.部分控件的Link属性链接
Pyqt有几个控件带有 setOpenExternalLinks , 如 QTextLabel 、 QTextBrowser 等
当 setOpenExternalLinks 值为TURE 表示可通过html 添加 A 标签打开外部链接, 如设置:
self.label.setOpenExternalLinks(True)
self.label.setText(_translate("MainWindow", "<html><head/><body><p><a href=\"http://www.baidu.com\"><span style=\" text-decoration: underline; color:#0000ff;\">百度首页</span></a></p></body></html>", None))
示例代码与效果:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'URL2.ui'
#
# Created: Fri Feb 06 10:03:54 2015
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui 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 Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(270, 313)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(80, 140, 101, 41))
self.label.setOpenExternalLinks(True)
self.label.setObjectName(_fromUtf8("label"))
self.openUrl = QtGui.QPushButton(self.centralwidget)
self.openUrl.setGeometry(QtCore.QRect(70, 40, 101, 21))
self.openUrl.setObjectName(_fromUtf8("openUrl"))
self.webbrowser = QtGui.QPushButton(self.centralwidget)
self.webbrowser.setGeometry(QtCore.QRect(70, 90, 101, 21))
self.webbrowser.setObjectName(_fromUtf8("webbrowser"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 270, 23))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.label.setText(_translate("MainWindow", "lalel属性效果", None))
self.openUrl.setText(_translate("MainWindow", "openUrl效果", None))
self.webbrowser.setText(_translate("MainWindow", "webbrowser效果", None)) class OpenUrl(QtGui.QMainWindow):
def __init__(self):
super(OpenUrl, self).__init__()
self.UI=Ui_MainWindow()
self.UI.setupUi(self)
self.setWindowTitle(u'Pyqt打开外部链接')
self.connect(self.UI.openUrl, QtCore.SIGNAL('clicked()'), self.openUrl)
self.connect(self.UI.webbrowser, QtCore.SIGNAL('clicked()'), self.webbrowser)
self.UI.label.setText(u'<a href="http://www.qq.com" style="color:#0000ff;"><b> 腾 讯 首 页 </b></a>') def openUrl(self):
QtGui.QDesktopServices.openUrl(QtCore.QUrl('http://www.hao123.com')) def webbrowser(self):
import webbrowser
webbrowser.open('http://www.sina.com.cn/') if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = OpenUrl()
MainWindow.show()
sys.exit(app.exec_())
效果: