I don’t exatly know what this code could be useful for, but I think it could be useful to make something for someone. This makes you open html files or web pages with a very rough interface. I made it some time ago just to open a chrome page with cefpython with a web address. Now I added the chance to open a local file. It can navigate in folders and show python code too. Have fun.
PS: it works with python 3.7, not with 3.8.2. (don’t wkon with python 3.8).
from cefpython3 import cefpython as cef import sys import tkinter as tk import os def open_link(url): "Shows a site or an html in the browser" sys.excepthook = cef.ExceptHook cef.Initialize() its_not_a_site = "https" not in url if its_not_a_site: url = f"{os.getcwd()}\\{url}" cef.CreateBrowserSync( url=url, window_title=url) cef.MessageLoop() def label(text, color="black"): lab = tk.Label( root, text=text, fg=color,) lab.pack() return lab def entry(): v = tk.StringVar() entry = tk.Entry( root, textvariable=v) entry.pack() v.set("http://www.google.com/") entry.focus() entry.bind("<Return>", lambda x: open_link(entry.get())) def listbox(): lst = tk.Listbox(root) for f in os.listdir(): lst.insert(0, f) lst.pack() lst.bind("<<ListboxSelect>>", lambda x: open_link(lst.get(lst.curselection()))) root.mainloop() cef.Shutdown() def main(): "The window to launch the browser with a file or url" label("Press Enter to launch the site", "red") entry() label("Click on a file to see it", "green") listbox() if __name__ == "__main__": root = tk.Tk() root.geometry("400x400") root.title("Tk browser") main()
The cefpython hello world example
This is the code for the hello world example on the documentation of cefpython
from cefpython3 import cefpython as cef import platform import sys def main(): check_versions() sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error cef.Initialize() cef.CreateBrowserSync(url="https://www.google.com/", window_title="Hello World!") cef.MessageLoop() cef.Shutdown() def check_versions(): ver = cef.GetVersion() print("[hello_world.py] CEF Python {ver}".format(ver=ver["version"])) print("[hello_world.py] Chromium {ver}".format(ver=ver["chrome_version"])) print("[hello_world.py] CEF {ver}".format(ver=ver["cef_version"])) print("[hello_world.py] Python {ver} {arch}".format( ver=platform.python_version(), arch=platform.architecture()[0])) assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this" if __name__ == '__main__': main()
This is the output
Cefpython3 repository
In case you want to clone the repository of cefpython3, go into a folder and in the cmd run this:
git clone https://github.com/cztomczak/cefpython.git
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame