How can I link to a webpage in a window using pyqt4?
I have a problem. Can I make a link on a web page in the window and when the user clicks on it, the web page will open in the browser. For example:
import sys
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
main = QtGui.QWidget()
main.setGeometry(200, 200, 200, 100)
label = QtGui.QLabel('<a href="http://stackoverflow.com/">Stackoverflow/</a>')
box = QtGui.QVBoxLayout()
box.addWidget(label)
main.setLayout(box)
main.show()
sys.exit(app.exec_())
It's really?
+3
source to share
2 answers
Of course you found the answer, but there is a special class that allows you to open a URL in the browser or default files in the default editors / players, etc. This is QDesktopServices
. For example:
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl
class MainWindow(QMainWindow, Ui_MainWindow):
def link(self, linkStr):
QDesktopServices.openUrl(QUrl(linkStr))
def __init__(self):
super(MainWindow, self).__init__()
# Set up the user interface from Designer.
self.setupUi(self)
self.label.linkActivated.connect(self.link)
self.label.setText('<a href="http://stackoverflow.com/">Stackoverflow/</a>')
This example is definitely more, but you should be aware of QDesktopServices
because it is a very useful class.
+7
source to share