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_())

效果:

Pyqt 打开外部链接的几种方法的更多相关文章

  1. 2015.1.25 Delphi打开网址链接的几种方法

    Delphi打开网址链接的几种方法1.使用shellapi打开系统中默认的浏览器              首先需在头部引用 shellapi单元即在uses中添加shellapi,这里我们需要知道有 ...

  2. Delphi中打开网页连接的几种方法

    https://blog.csdn.net/zisongjia/article/details/69398143 正好要用,做个记录.Mark下. 使用了第一种 uses shellapi proce ...

  3. 痞子衡嵌入式:MCUXpresso IDE下将应用程序RW段分散链接的几种方法

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是MCUXpresso IDE下将应用程序RW段分散链接的几种方法. 早期的 MCU 芯片,一般都会嵌入内部 Flash 和 RAM,并且 ...

  4. 打开cad文件的几种方法

    转自原文 打开cad文件的几种方法 IWorkspaceFactory pWorkspaceFactory; IFeatureWorkspace pFeatureWorkspace; IFeature ...

  5. Response.Redirect 打开新窗体的两种方法

    普通情况下,Response.Redirect 方法是在server端进行转向,因此,除非使用 Response.Write("<script>window.location=' ...

  6. 获取当前页面的所有链接的四种方法对比(python 爬虫)

    ''' 得到当前页面所有连接 ''' import requests import re from bs4 import BeautifulSoup from lxml import etree fr ...

  7. Response.Redirect 打开新窗口的两种方法

    一般情况下,Response.Redirect 方法是在服务器端进行转向,因此,除非使用 Response.Write("<script>window.location='htt ...

  8. VS2005打开VS2008项目的2种方法

    vs2008支持.net3.5,而vs2005支持.net2.0,所以使用vs2005打开vs2008的项目,要确定你的项目是.net2.0的. 下面介绍2种方法: 方法1:用记事本打开.sln文件, ...

  9. Win7系统打开服务管理界面的几种方法汇总

    转自:https://www.jb51.net/os/windows/318465.html Win7服务管理包含了计算机操作系统和应用程序提供的所有服务,但是这么多服务并非总是用户所需的.比如打印机 ...

随机推荐

  1. HDU 3999 二叉排序树

    The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...

  2. centos 终端 字体颜色

    默认情况下,没有颜色. https://www.centos.org/docs/2/rhl-gsg-en-7.2/ls-color.html git也默认没有颜色,破解如下: git config - ...

  3. Ext treelist 动态切换TreeStore

    chooseMenu: function(_this) { //var mycomp = top.Ext.getCmp("my_comp"); var menuTreeStore ...

  4. (转)Do not use "using" for WCF Clients - 不要将WCF Client 放在 ‘Using’ 代码块中

    RT,最近在编写WCF的应用程序,发现WCF client在关闭的时候有可能会抛出异常,经过搜索之后,发些小伙伴们也遇到过类似的问题,遂记载下来,以备自身和其他小伙伴查看. 原文链接:http://w ...

  5. Struts2学习笔记《一》

    (一)struts2中JSP页面数据与Action对应的三种方式 1.属性驱动,及页面中带有name属性的值传递过来action中,struts2会将值直接映射到getter.setter接收器中 此 ...

  6. word to word

    Question: For each word, you can get a list of neighbor words by calling getWords(String), find all ...

  7. IPC---共享内存

    共享内存就是允许两个或多个不相关的进程访问同一个逻辑内存.共享内存是在两个正在运行的进程之间共享和传递数据时,不需要在客户进程和服务器进程之间幅值,因此是最快的一种IPC.不同进程之间共享的内存通常安 ...

  8. java web 学习 --第五天(Java三级考试)

    第四天的学习内容:http://www.cnblogs.com/tobecrazy/p/3454860.html Response对象 response对象主要是向客户端浏览器发送二进制数据,如输出C ...

  9. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  10. Java for LeetCode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...