Python Forum
[Tkinter] can i had a cefpython3 to a labelframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] can i had a cefpython3 to a labelframe
#1
Im having some dificulties setting the browser window in a LabelFrame on the right, can someone help?

from tkinter import *
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
import tkinter as tk
from formularios import tabmain0
import requests
from bs4 import BeautifulSoup
import webbrowser

def home():
container = ttk.Frame(tabmain0)
canvas = tk.Canvas(container)
scrollbar_x = ttk.Scrollbar(container, orient="horizontal", command=canvas.xview)
scrollbar_y = ttk.Scrollbar(container, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas)

scrollable_frame.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))

canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")

canvas.configure(xscrollcommand=scrollbar_x.set)
canvas.configure(yscrollcommand=scrollbar_y.set)

stathomelib = LabelFrame(scrollable_frame, text="Library", padx=5, pady=5, highlightbackground="black",
highlightthickness=2)
stathomelib.grid(row=3, column=0, columnspan=2, sticky=NW, pady=2)

stathomedoc = LabelFrame(scrollable_frame, text="Documentation", padx=5, pady=5, highlightbackground="black",
highlightthickness=2)
stathomedoc.grid(row=3, column=2, columnspan=2,sticky=NW, pady=2)

stathomeTools = LabelFrame(scrollable_frame, text="External Tools", padx=5, pady=5, highlightbackground="black",
highlightthickness=2)
stathomeTools.grid(row=3, column=4, columnspan=2, sticky=NW, pady=2)


# this is were i whant the browser window to show
home_browser = LabelFrame(scrollable_frame, text="", padx=5, pady=5, highlightbackground="black",
highlightthickness=2)
home_browser.grid(row=0, column=99, rowspan=99, sticky='ne', pady=2)
valido_browser = tk.IntVar()
valido_browser.set(False)

checkbrowser = tk.Checkbutton(home_browser, fg="blue", selectcolor="red", text="Open in Browser", variable=valido_browser)
checkbrowser.grid(row=0, column=0, columnspan=9, sticky="wn")



def choose_browser(urlbook):
if valido_browser.get() == 1:
webbrowser.open(urlbook)
else:
home_browser.insert(tk.INSERT, urlbook)

global varLnkOrder
varLnkOrder = ""
varLbook = open("Confs/bookmarks.txt", "r").readlines()
try:
for line in varLbook:
undef = ""
if ":stathomelib" in line:
varLnkOrder = stathomelib
taman=43
c = 0
i = 0

elif ":stathomedoc" in line:
varLnkOrder = stathomedoc
taman=43
c = 0
i = 0


elif ":stathomeTools" in line:
varLnkOrder = stathomeTools
taman = 20
c = 0
i = 0



if len(line) > 1:

titul, urlbook = line.split('<=>')
if len(titul) > 1:
link1 = Label(varLnkOrder, width=taman, text=titul, justify="left", anchor="center", fg="blue", cursor="draft_large")
link1.grid(row=i, column=c, sticky="n", pady=2)
link1.bind("<Button-1>", lambda e, urlbook=urlbook: choose_browser(urlbook.rstrip()))
i += 1
line == ""



container.pack(expand=1, fill="both")
scrollbar_x.pack(side="bottom", fill="x")
scrollbar_y.pack(side="right", fill="y")
canvas.pack(side="bottom", fill="both", expand=True)
im testin in here

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

from cefpython3 import cefpython as cef
import platform
import sys
import tkinter as tk
from selenium import webdriver
import logging as _logging
import undetected_chromedriver.v2 as uc



options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = uc.Chrome()


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():
    logger = _logging.getLogger("tkinter_.py")
    logger.setLevel(_logging.DEBUG)
    stream_handler = _logging.StreamHandler()
    formatter = _logging.Formatter("[%(filename)s] %(message)s")
    stream_handler.setFormatter(formatter)
    logger.addHandler(stream_handler)
    logger.info("CEF Python {ver}".format(ver=cef.__version__))
    logger.info("Python {ver} {arch}".format(
        ver=platform.python_version(), arch=platform.architecture()[0]))
    logger.info("Tk {ver}".format(ver=tk.Tcl().eval('info patchlevel')))
    assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this"
    sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
    # Tk must be initialized before CEF otherwise fatal error (Issue #306)
    global root
    root = tk.Tk()
    root.geometry("400x100")
    frm1 = ttk.Frame(root)
    frm1.pack()
    frmbrowser = ttk.Frame(root)
    frmbrowser.pack()
    open_link("https://www.google.com/")








    # Platforms
    WINDOWS = (platform.system() == "Windows")
    LINUX = (platform.system() == "Linux")
    MAC = (platform.system() == "Darwin")

    settings = {}
    if MAC:
        settings["external_message_pump"] = True
    cef.Initialize(settings=settings)
    logger.debug("Main loop exited")
    root.mainloop()
    cef.Shutdown()


if __name__ == '__main__':
    main()
Reply
#2
cefpython has a tkinter example
https://github.com/cztomczak/cefpython/b...kinter_.py
Larz60+ likes this post
Reply
#3
(Aug-28-2021, 09:03 PM)Yoriz Wrote: cefpython has a tkinter example
https://github.com/cztomczak/cefpython/b...kinter_.py

i know, iv tried but its very confusing, i cant seem to point the browser to the labelframe, thats why im asking for help :(
Reply
#4
This is the important part:
window_info.SetAsChild(self.get_window_handle(), rect)
This tells the browser to make it's window to a child of the Frame window. Most of the rest of the code in BrowserFrame is telling the BrowserFrame how to be a parent of a browser. Let the browser know when the window changes size or is moved. Let the browser know when the frame gets or loses focus. Let the browser know when the frame goes away. I don't think there is a much simpler way to do all that than the example code. Usually tkinter creates all the windows and everything that BrowserFrame does takes place behind the scenes.

What parts in particular do you find confusing?
Reply
#5
(Aug-30-2021, 08:26 PM)deanhystad Wrote: This is the important part:
window_info.SetAsChild(self.get_window_handle(), rect)
This tells the browser to make it's window to a child of the Frame window. Most of the rest of the code in BrowserFrame is telling the BrowserFrame how to be a parent of a browser. Let the browser know when the window changes size or is moved. Let the browser know when the frame gets or loses focus. Let the browser know when the frame goes away. I don't think there is a much simpler way to do all that than the example code. Usually tkinter creates all the windows and everything that BrowserFrame does takes place behind the scenes.

What parts in particular do you find confusing?


im having dificulties specifing the frame where the browser window opens.
for example:

root = Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))

leftfrm = LabelFrame(root, text="", padx=5, pady=5, highlightbackground="black",highlightthickness=2)
leftfrm.grid(row=0, column=0, sticky='ne', pady=2)

#i whant the browser to open in this frame
home_browser = LabelFrame(root, text="", padx=5, pady=5, highlightbackground="black",highlightthickness=2)
home_browser.grid(row=0, column=1, sticky='ne', pady=2)

rightfrm = LabelFrame(root, text="", padx=5, pady=5, highlightbackground="black",highlightthickness=2)
rightfrm.grid(row=0, column=2, sticky='ne', pady=2)


preferebly called by a function, for example:
func_open_browser("home_browser", "http://www.google.com")

thanks




this code also works for me i gues it would be easier to implement.

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="test")
    cef.MessageLoop()
    cef.Shutdown()
def check_versions():
    assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this"
if __name__ == '__main__':
    main()
Reply
#6
The browser is not a tkinter widget. It cannot be added to a label frame. You need a specialized widget that understands how to host a browser window. That is what the example shows, and it shows it in a fairly concise manner. If there was a two step process for displaying a browser in tkinter don't you think the example would show that?

Have you tried running the example code? That would be my first step. Forget about your program for now and just try running the browser example as is. Does it work like you think? What happens when you resize the window, does the browse resize? Do you like navigation bar? Maybe you'll want that for your program too.

Once you have the example running think about how you can use the example code in your project. The example defines a specialized frame for housing a browser window. Your code can import the example as a module and use that class. Create an instance of "BrowserFrame" and pack it in your label frame. The example code adds the browser to the NavigationBar.
        self.browser_frame = BrowserFrame(self, self.navigation_bar)
        self.browser_frame.grid(row=1, column=0,
                                sticky=(tk.N + tk.S + tk.E + tk.W))
        tk.Grid.rowconfigure(self, 1, weight=1)
        tk.Grid.columnconfigure(self, 0, weight=1)
And the NavigationBar is added to the MainFrame (just a frame that fills the entire root window).
        self.navigation_bar = NavigationBar(self)
        self.navigation_bar.grid(row=0, column=0,
                                 sticky=(tk.N + tk.S + tk.E + tk.W))
If you don't want the Navigation bar I think you could just create an instance of BrowserFrame and add it to a LabelFrame.
label_frame = tk.LabelFrame(root, text='Browser')
label_frame.grid(row=0, column=0, sticky=(tk.N + tk.S + tk.E + tk.W))
tk.Grid.rowconfigure(label_frame , 1, weight=1)
tk.Grid.columnconfigure(label_frame , 0, weight=1)

browser_frame = BrowserFrame(self, label_frame)
browser_frame.grid(row=1, column=0, sticky=(tk.N + tk.S + tk.E + tk.W))
tk.Grid.rowconfigure(browser_frame , 1, weight=1)
tk.Grid.columnconfigure(browser_frame , 0, weight=1)
You probably won't need to make a scrollable frame because the browser will do that for you automatically. I have not run the example, so I don't know for sure.
Reply
#7
im sorry, im lost Sad

this works

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




def main():
    sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
    cef.Initialize()
    cef.CreateBrowserSync(url="https://www.google.com/", window_title="test")
    cef.MessageLoop()
    cef.Shutdown()



if __name__ == '__main__':
    main()
but if i even trie to create a window with root=tk.TK() it crashes Wall

im lost, tried what you said, but if the above example above works like i need it, i just need it to be inside a labelframe or frame Sad
Reply
#8
Did you run the example Yoriz referenced?
Reply
#9
(Sep-01-2021, 12:47 PM)deanhystad Wrote: Did you run the example Yoriz referenced?

yes, i have already been trying to tinckle with it
Reply
#10
Did it run without any changes?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,479 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  Issue in Tkinter with winfo_class() and LabelFrame ReDefendeur 1 2,706 Oct-05-2020, 05:52 AM
Last Post: Jeff900

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020