Pyqt 打开外部链接的几种方法
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 打开外部链接的几种方法的更多相关文章
- 2015.1.25 Delphi打开网址链接的几种方法
Delphi打开网址链接的几种方法1.使用shellapi打开系统中默认的浏览器 首先需在头部引用 shellapi单元即在uses中添加shellapi,这里我们需要知道有 ...
- Delphi中打开网页连接的几种方法
https://blog.csdn.net/zisongjia/article/details/69398143 正好要用,做个记录.Mark下. 使用了第一种 uses shellapi proce ...
- 痞子衡嵌入式:MCUXpresso IDE下将应用程序RW段分散链接的几种方法
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是MCUXpresso IDE下将应用程序RW段分散链接的几种方法. 早期的 MCU 芯片,一般都会嵌入内部 Flash 和 RAM,并且 ...
- 打开cad文件的几种方法
转自原文 打开cad文件的几种方法 IWorkspaceFactory pWorkspaceFactory; IFeatureWorkspace pFeatureWorkspace; IFeature ...
- Response.Redirect 打开新窗体的两种方法
普通情况下,Response.Redirect 方法是在server端进行转向,因此,除非使用 Response.Write("<script>window.location=' ...
- 获取当前页面的所有链接的四种方法对比(python 爬虫)
''' 得到当前页面所有连接 ''' import requests import re from bs4 import BeautifulSoup from lxml import etree fr ...
- Response.Redirect 打开新窗口的两种方法
一般情况下,Response.Redirect 方法是在服务器端进行转向,因此,除非使用 Response.Write("<script>window.location='htt ...
- VS2005打开VS2008项目的2种方法
vs2008支持.net3.5,而vs2005支持.net2.0,所以使用vs2005打开vs2008的项目,要确定你的项目是.net2.0的. 下面介绍2种方法: 方法1:用记事本打开.sln文件, ...
- Win7系统打开服务管理界面的几种方法汇总
转自:https://www.jb51.net/os/windows/318465.html Win7服务管理包含了计算机操作系统和应用程序提供的所有服务,但是这么多服务并非总是用户所需的.比如打印机 ...
随机推荐
- 剑指Offer 二叉搜索树的后序遍历序列
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路: 后续遍历数组的尾部为根节点,前面的部分 ...
- auto与decltype
今天搜狗笔试的一道选择题,原题给忘了,但记得所考的知识点.知识点很基础,但很容易忽视. 具体内容可参考C++ Primer. auto :变量取auto后,其所对应的类型 auto一般会 ...
- 2016年11月2日--Window.document对象
一.找到元素: docunment.getElementById("id"): 根据id找,最多找一个: var a =docunment ...
- HTTP协议概念篇
1.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...
- 9.8---硬币问题(CC150)
这道题卡了一天.要想AC非常难. 1,第一个解决办法,优化暴力: public class Coins { public static int countWays(int n){ int num25 ...
- [k]web页面-browser兼容问题-1
1:空的a标签在IE7/8下不能点击(2015-05-22) html代码: <ul class='oUl'><li><a href="#"> ...
- 【leetcode】Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- ios cordite 读取错误CoreData could not fulfill a fault for '0x15b4a870
解释在这里 http://stackoverflow.com/questions/14296892/nsobjectinaccessibleexception-reason-coredata-coul ...
- Solr集群更新配置的方式
solr集群中配置文件是经常更新的,频率最高的也就是schema.xml和solrconfig.xml这两个配置文件了,对于更新配置文件之前,我们先了解一下集群项目结构 由于在集群模式下,solrco ...
- MongoDB 数据库管理(不定时更新)
之前的几篇文章大致说了副本集的搭建.副本集的管理,现在说下MongoDB数据库的管理.数据库管理包括:备份.还原.导入.导出.服务器管理等. 一:查看服务器状态,查看命令行参数.db.serverSt ...