Add web browser window to Tkinter window [closed]
Asked Answered
O

3

8

I made a gui app with python tkinter, I want to put a little mini little web browser inside my tKinter windown, without opening a new tab. How can I do that?

Olmos answered 21/9, 2018 at 3:33 Comment(0)
F
4

You can't. There is no widget in the tkinter library which allow you to display HTML. If you really NEED to use tkinter you could try tkinterhtml but it's weird and won't work for a lot of features (hyperlinks, images...)

If you just want to create a python app with a web interface, you'd better use a python web framework like flask or django.

Fining answered 17/12, 2018 at 15:15 Comment(0)
R
6

This is an old question but for anyone stumbling across this in the future, embedding webpages in tkinter is possible.

You could try tkinterweb (Disclaimer: I am the author). It is based on tkinterhtml but supports hyperlinks, styling, images, etc. Simply install it using pip install tkinterweb, and run:

import tkinterweb
import tkinter as tk
root = tk.Tk()
frame = tkinterweb.HtmlFrame(root)
frame.load_website(YOUR_WEBSITE)
frame.pack(fill="both", expand=True)
root.mainloop()

Tkinterweb runs off of a discontinued Tk HTML widget, so complex websites might not work properly. Simpler websites work perfectly fine, though.

Another great alternative is cefpython, which is a full-blown chromium browser embedded in Tkinter. An example of using cefpython in tkinter can be found at https://github.com/cztomczak/cefpython/blob/master/examples/tkinter_.py. Unfortunately, cefpython does have some issues with newer python versions and can be a bit resource-consuming, but it does work really well.

If you need a complete webbrowser in your Tkinter app, I suggest you try cefpython. If you just need something really fast and simple to show basic webpages or help windows, try tkinterweb.

Rotberg answered 12/1, 2021 at 20:21 Comment(8)
I tried tkinterweb, but it stops the code even before tkinter window pops-up. Here is my code: import tkinterweb import tkinter as tk root = tk.Tk() frame = tkinterweb.HtmlFrame(root) frame.load_website("https://www.google.com") frame.place(0, 0) root.mainloop() The output is: UserNotification: Fetching Tkhtml3 for 64-bit Windows with Python 3.7.9. Tkhtml3 found in C:\Users\ymzym.DESKTOP-TQFQIM2\AppData\Local\Programs\Python\Python37\lib\site-packages\tkinterweb\tkhtml\Windows\64-bit. UserNotification: Connecting to www.google.com.Althaalthea
Hello, thanks for letting me know about this. It looks like you are using 64-bit Windows. There is a known Microsoft C Compiler bug that is causing TkinterWeb to crash on certain 64-bit Windows platforms. This is currently being worked on and will hopefully be solved soon. You can find details at github.com/Andereoo/TkinterWeb/issues/2.Rotberg
@YılmazAlpaslan The issue with 64-bit Windows TkinterWeb has been resolved. Consider re-installing TkinterWeb.Rotberg
I am glad to hear that. I'll reinstall TkinterWeb. Have a good day!Althaalthea
@Andoo, how do you open a local file with tkinterweb? I'm trying to open a browser with tkinterweb that runs my html codeTorrid
You can use yourframe.load_file("/path/to/your/file.html") to load HTML from a local file. You can also use yourframe.load_html("<p>your HTML here</p>") to load HTML from a string. Check out github.com/Andereoo/TkinterWeb/blob/main/tkinterweb/docs/… for more info.Rotberg
@Andoo Does tkinterweb run embedded JS and CSS?Roan
@Roan TkinterWeb can run embedded CSS. A full list of supported CSS declarations can be found at tkhtml.tcl.tk/support.html. Unfortunately, TkinterWeb cannot not run JS. You can, however interact with the document with Python, although I admit it is not particularly straightforward (let me know if you want a hand with that).Rotberg
M
6

I know this is an old question, but I would suggest using PyQt5 if you want to create web browser windows.

Here is a basic implementation that will open a custom browser to the google home page:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl('http://google.com'))
        self.setCentralWidget(self.browser)
        self.showMaximized()
app = QApplication(sys.argv)
QApplication.setApplicationName('Example Custom Browser')
window = MainWindow()
app.exec_()

This browser is pretty useless, other than for performing google searches, but you can add a navbar with functional buttons with very little extra effort. One basic way could look like this:

        navbar = QToolBar()
        self.addToolBar(navbar)

        back_btn = QAction('Back', self)
        back_btn.triggered.connect(self.browser.back)
        navbar.addAction(back_btn)

        forward_btn = QAction('Forward', self)
        forward_btn.triggered.connect(self.browser.forward)
        navbar.addAction(forward_btn)

        reload_btn = QAction('Reload', self)
        reload_btn.triggered.connect(self.browser.reload)
        navbar.addAction(reload_btn)

        home_btn = QAction('Home', self)
        home_btn.triggered.connect(self.navigate_home)
        navbar.addAction(home_btn)

        self.url_bar = QLineEdit()
        self.url_bar.returnPressed.connect(self.navigate_to_url)
        navbar.addWidget(self.url_bar)

        self.browser.urlChanged.connect(self.update_url)

    def navigate_home(self):
        self.browser.setUrl(QUrl('https://www.google.com'))

    def navigate_to_url(self):
        url = self.url_bar.text()
        self.browser.setUrl(QUrl(url))

    def update_url(self, q):
        self.url_bar.setText(q.toString())

If you add this to the MainWindow class, you should now see the buttons 'Back', 'Forward', 'Reload' and 'Home' in the top left corner. They do what you might expect them to, which is take you one page forwards, backwards, reload the page, or take you home (I just set it to google.com).

Sorry this is so long, but I hope it demonstrates how to simply implement a PyQt5 browser window.

Manhood answered 1/11, 2021 at 22:2 Comment(0)
F
4

You can't. There is no widget in the tkinter library which allow you to display HTML. If you really NEED to use tkinter you could try tkinterhtml but it's weird and won't work for a lot of features (hyperlinks, images...)

If you just want to create a python app with a web interface, you'd better use a python web framework like flask or django.

Fining answered 17/12, 2018 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.