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?
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.
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.
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 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.
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.
© 2022 - 2024 — McMap. All rights reserved.
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