Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make link on web page in window using pyqt4?

I have a problem. Can I make a link on the web page in the window and when the user clicks on it, the web page will be 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_())

Is it really?

like image 831
Q-bart Avatar asked May 01 '15 12:05

Q-bart


People also ask

Can you create a hyperlink in Python?

There's no such thing as a hyperlink in a text file. A text file just has text. You can write syntax for a hyperlink, say using HTML -- is that what you mean? @katrielalex: So you mean that I should create logfile.

How do you print a link in Python?

If you print a URL to the console, it will get underlined if you hover over it, and if you right-click on it a menu pops up, and one of the menu options is "Open link". You just print it syntactically correct. To identify the hyperlink is the job of the terminal application. – Klaus D.

What is PyQT used for?

PyQt is widely used for creating large-scale GUI-based programs. It gives programmers the freedom to create GUIs of their choice while also providing a lot of good pre-built designs. PyQT gives you widgets to create complex GUIs.

Is PyQT drag and drop?

The provision of drag and drop is very intuitive for the user. It is found in many desktop applications where the user can copy or move objects from one window to another. MIME based drag and drop data transfer is based on QDrag class.


2 Answers

It is of course good that you found answer, but there is special class, which allows you to open URL in default browser or files in default editors/players etc. It 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 larger, but you should know about QDesktopServices, because it is very useful class.

like image 124
Kosovan Avatar answered Oct 04 '22 17:10

Kosovan


Sorry. I have already searched the answer.

label.setText('<a href="http://stackoverflow.com/">Link</a>')
label.setOpenExternalLinks(True)
like image 34
Q-bart Avatar answered Oct 04 '22 17:10

Q-bart