Hi, i have this script but cant seem to open the browserframe in the correct place

Captura_de_ecrã_2021-10-25,_às_11_07_07.png

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

# platforms
WINDOWS = platform.system() == 'Windows'
LINUX = platform.system() == 'Linux'
MAC = platform.system() == 'Darwin'


class BrowserFrame(tk.Frame):
    def __init__(self, master=None, **kw):
        super().__init__(master, **kw)
        self.browser = None
        self.bind('<Configure>', self.on_configure)

    def get_window_handle(self):
        if MAC:
            from AppKit import NSApp
            import objc
            return objc.pyobjc_id(NSApp.windows()[-1].contentView())
        elif self.winfo_id() > 0:
            return self.winfo_id()
        else:
            raise Exception('Could not obtain window handle!')

    def on_configure(self, event):
        if self.browser is None:
            # create the browser and embed it in current frame
            rect = [0, 0, self.winfo_width(), self.winfo_height()]
            cef_winfo = cef.WindowInfo()
            win_id = self.get_window_handle()
            cef_winfo.SetAsChild(win_id, rect)
            self.browser = cef.CreateBrowserSync(cef_winfo, url=urlset)

            # start the browser handling loop
            self.cef_loop()

        # resize the browser
        if WINDOWS:
            ctypes.windll.user32.SetWindowPos(
                self.browser.GetWindowHandle(), 0,
                0, 0, event.width, event.height, 0x0002)
        elif LINUX:
            self.browser.SetBounds(0, 0, event.width, event.height)

    def cef_loop(self):
        cef.MessageLoopWork()
        self.after(10, self.cef_loop)


def main():
    sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error

    root = tk.Tk()
    root.minsize(600, 600)
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    root.geometry("%dx%d+0+0" % (w-550, h-250))
    root.title('Test')

    settings = {}
    if MAC:
        settings["external_message_pump"] = True

    cef.Initialize(settings=settings)

    leftfrm = LabelFrame(root, text="left frame for buttons with links", padx=5, pady=5, highlightbackground="black", highlightthickness=2)
    leftfrm.grid(row=0, column=0, rowspan=99, sticky='nw', pady=2)

    home_browser = LabelFrame(root, text="right frame for browser", padx=5, pady=5, highlightbackground="black", highlightthickness=2)
    home_browser.grid(row=0, column=1, rowspan=99, sticky='ne', pady=2)

    BrowserFrame(home_browser).grid(row=2, column=0, columnspan=9, sticky="ne")

    for x in range(1, 25):
        Label(leftfrm, text=str("Link "+str(x))).grid(row=x,column=0)

    global urlset
    urlset = "http://www.google.com"

    root.mainloop()

if __name__ == '__main__':
    main()

The browser only opens when i resize the window and not in the correct place.
Im using a Mac by the way.

thanks

I do not have an Apple to test on but line 42 and forward I don't see code for MAC. Why?

commented: its the def get_window_handle(self): +3

Again, line 42 calls out that this is where the resize occurs yet I can't find the code for MAC? Why?

commented: that are exception for windows and linux, for mac its define on the def get_window_handle(self): +0

i think the issue is here

rect = [0, 0, self.winfo_width(), self.winfo_height()]

it prints

140339762794416 [0, 0, 1, 1]

i think its because the browser windows is not set properly, the self points to .!labelframe2.!browserframe

if i replace

rect = [0, 0, self.winfo_width(), self.winfo_height()]
for
rect = [0, 0, 300,200]

it opens, so the issue is that the browser frame is set to 0,0

also, doing this opens but not in the correct LabelFrame(home_browser)

also this might help

print(win_id, rect) -> 140276467161232 [5, 5, 1, 1]
print(self) -> .!labelframe2.!browserframe
print(cef_winfo) -> <cefpython_py39.WindowInfo object at 0x7f94ac924220>
print(event) -> <Configure event x=9 y=25 width=1 height=1>

If there is code that the comment write "resize occurs here" then on review I expect the resize code to be there. Yet it is not?

Uncommented code means you and God knows how it works. Later, only God knows.

Now its pointing right and opening with the specified dimensions, but it still not opening in the correct place although self is pointing to the correct frame

    import ctypes
    import platform
    from cefpython3 import cefpython as cef
    from tkinter import *
    import tkinter as tk
    import sys
    from cefpython.examples.tkinter_ import logger


    WINDOWS = platform.system() == 'Windows'
    LINUX = platform.system() == 'Linux'
    MAC = platform.system() == 'Darwin'

    class BrowserFrame(tk.LabelFrame):
        def __init__(self, master=None, **kw):
            super().__init__(master, text='Browser', **kw)
            self.browser = None
            self.bind('<Configure>', self.on_configure)

        def winfo_id(self):
            return super.winfo_id()

        def get_window_handle(self):
            if MAC:
                from AppKit import NSApp
                import objc
                return objc.pyobjc_id(NSApp.windows()[-1].contentView())
            elif self.winfo_id() > 0:
                return self.winfo_id()
            else:
                raise Exception('Could not obtain window handle!')

        def cef_loop(self):
            cef.MessageLoopWork()
            self.after(10, self.cef_loop)


        def on_configure(self, event):
            if self.browser is None:
                # create the browser and embed it in current frame
                self.update()
                rect = [0, 0, 500, 450]
                cef_winfo = cef.WindowInfo()
                win_id = self.get_window_handle()
                cef_winfo.SetAsChild(win_id, rect)
                self.browser = cef.CreateBrowserSync(cef_winfo, url=urlset)


                print("self: ",self)
                print("rect: ",rect)
                print("win_id: ",win_id)
                print("self.winfo_width(), self.winfo_height(): ",self.winfo_width(), self.winfo_height())
                print("self.browser: ",self.browser)
                print("cef_winfo: ",cef_winfo)

                # start the browser handling loop
                self.cef_loop()

            # resize the browser
            if WINDOWS:
                ctypes.windll.user32.SetWindowPos(
                    self.browser.GetWindowHandle(), 0,
                    0, 0, event.width, event.height, 0x0002)
            elif LINUX:
                self.browser.SetBounds(0, 0, event.width, event.height)




    def main():
        root = tk.Tk()
        root.columnconfigure(0, weight=1)
        root.columnconfigure(1, weight=2)
        root.rowconfigure(0, weight=1)
        w, h = root.winfo_screenwidth(), root.winfo_screenheight()
        root.geometry("%dx%d+0+0" % (w-250, h-300))
        root.title('Test')


        WindowUtils = cef.WindowUtils()
        sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
        settings = {}
        if MAC:
            settings["external_message_pump"] = True

        cef.Initialize(settings=settings)


        leftfrm = tk.LabelFrame(root, text="Left", padx=5, pady=5, bg='red')
        leftfrm.grid(row=0, column=0, sticky='news', pady=2)

        home_browser = tk.LabelFrame(root, text="home_of_the_browser", padx=5, pady=5, bg='blue')
        home_browser.grid(row=0, column=1, pady=2,ipady=200,ipadx=400)
        home_browser.columnconfigure(1, weight=1)
        home_browser.rowconfigure(0, weight=1)


        browser_frame = BrowserFrame(home_browser, bg='green')
        browser_frame.grid(row=0, column=1, sticky=('new'))
        browser_frame.columnconfigure(1, weight=1)
        browser_frame.rowconfigure(0, weight=1)

        print("->", leftfrm) #labelframe
        print("->", home_browser) #labelframe2
        print("->", browser_frame) #!labelframe2.!browserframe

        for x in range(1, 25):
            tk.Label(leftfrm, text=f"Link {x}", bg='yellow').grid(row=x, column=0)

        global urlset
        urlset = "http://www.google.com"




        root.mainloop()


    if __name__ == '__main__':
        main()

-> .!labelframe
-> .!labelframe2
-> .!labelframe2.!browserframe
self: .!labelframe2.!browserframe
rect: [0, 0, 500, 450]
win_id: 140594983878432
self.winfo_width(), self.winfo_height(): 938 1
self.browser: <cefpython_py39.PyBrowser object at 0x7fded33963c0>
cef_winfo: <cefpython_py39.WindowInfo object at 0x7fded11f0450>

Captura_de_ecrã_2021-10-27,_às_12_42_07.png

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.