Search code examples
pythonpyqt5python-3.9

Is there any way to display an input html tag in a QTextBrowser PyQt5?


I am using Python 3.9 and PyQt5 and I am trying to display an input html tag in a QTextBrowser widget. When I try to add an input tag the html page looks blank when I view it in the QTextBrowser. Is there any way that I could add an input tag to the QTextBrowser? This is what my HTML code looks like.

<html>
<body>
<input type="text"></input>
</body>
</html>

Solution

  • Unfortunately QTextBrowser only handles a limited subset of HTML4 elements and among them there is no input.

    One possible solution is to use QWebEngineView:

    import sys
    
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtWebEngineWidgets import QWebEngineView
    
    html = """
    <!DOCTYPE html>
    <html>
    <body>
    <h1>The input element</h1>
    <label for="fname">First name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last name:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <input type="submit" value="Submit">
    </body>
    </html>
    """
    
    
    def main():
        app = QApplication(sys.argv)
    
        view = QWebEngineView()
        view.setHtml(html)
        view.resize(640, 480)
        view.show()
    
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()