Python goes to the web: cefpython and tkinter

Update: how to load a local file

I found that you can also run a local file in the browser. To do it you need to pass to cef.CreatebrowserLink this:

"file:///index.html"

As, in the code below, I pass this argument throug the entry box linked to the v StringVar, you have to change that into this line:

v.set("file:///index.html")

You could also check that if there is no https or www at the beginning, it is a local file, so that you add file:/// at the start.

What is cefpython 3

With cefpython 3 you can open chrome “into” python and surf the web. With this tiny example (a modification of the hello_world.py example from the authors of the module), I show you how to make a very basic application to surf the web inserting the address in an entry (in a window made with tkinter). From this start, who knows where we can go?

This is the window to insert the address:

And this is the page that opens into the browser

The title of the window will be the address

When you close this page, the address inserter window will appear again to add another site. If you shut down this window the program will definitively shut down.

The code

# Hello world example. Doesn't depend on any third party GUI framework.
# Tested with CEF Python v57.0+.

from cefpython3 import cefpython as cef
import platform
import sys
import tkinter as tk


def open_link(url):
    print(url)
    root.destroy()
    sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
    cef.Initialize()
    cef.CreateBrowserSync(url=url,
        window_title=url)
    cef.MessageLoop()
    main()

def main():
    global root
    root = tk.Tk()
    root.geometry("400x100")
    l = tk.Label(root, text="Press Enter to browse Internet", fg="blue", font="Arial 20")
    l.pack(fill=tk.X)
    v = tk.StringVar()
    e = tk.Entry(root, textvariable=v, font="Arial 14")
    e.pack(fill=tk.X)
    v.set("https://www.google.com/")
    e.focus()
    e.bind("<Return>", lambda x: open_link(e.get()))
    root.mainloop()
    cef.Shutdown()


if __name__ == '__main__':
    main()

Let’s see a video to know how it works

 

Create html in the script, save it and open it in chrome

With this script you can your chrome core with cefpython3, creating the html code into the page of the script itself and then launching it.

from cefpython3 import cefpython as cef
import sys
import os


def open_link(url):
    sys.excepthook = cef.ExceptHook
    cef.Initialize()
    cef.CreateBrowserSync(url=url, window_title=url.replace("file:///",""))
    cef.MessageLoop()


if __name__ == '__main__':
    html = """<h3>Welcome to my site</h3>
<img src="https://lh6.googleusercontent.com/-STVPXBxpfbk/AAAAAAAAAAI/AAAAAAAAAMM/7wmzAdslc70/photo.jpg?sz=48" />
Hi, come to check my blog <a href="https://pythonprogramming.altervista.org">pythonprogramming.altervista.org</a>
"""
    name = "my.html"
    # if the file already exists, it will not be created again
    if name not in os.listdir():
        with open(name, "w") as file:
            file.write(html)
    open_link("file:///" + name)

 

Utility links

Utilities

Tkinter links

Tkinter test for students

Tkinter articles

Pygame links

Pygame's Platform Game

Other Pygame's posts

t

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.