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
#21
I think this may be a tkinter layout problem. What is the location and size of the BrowserFrame?
Reply
#22
(Oct-25-2021, 05:51 PM)deanhystad Wrote: I think this may be a tkinter layout problem. What is the location and size of the BrowserFrame?



i think thats the issue, its not pointing to the frame and so it load width and height =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>
Reply
#23
I removed all the cef stuff.
import tkinter as tk
import sys

class BrowserFrame(tk.LabelFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, text='Browser', **kwargs)
        self.bind('<Configure>', self.on_configure)

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

    def on_configure(self, event):
        print('on_configure', event.width, event.height)


def main():
    root = tk.Tk()
    leftfrm = tk.LabelFrame(root, text="Left", padx=5, pady=5, bg='red')
    leftfrm.grid(row=0, column=0, rowspan=99, sticky='nw', pady=2)

    home_browser = tk.LabelFrame(root, text="Home", padx=5, pady=5, bg='blue')
    home_browser.grid(row=0, column=1, rowspan=99, sticky='ne', pady=2)

    browser_frame = BrowserFrame(home_browser, bg='green')
    browser_frame.grid(row=1, column=0, sticky=(tk.N + tk.S + tk.E + tk.W))

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

    root.mainloop()

if __name__ == '__main__':
    main()
When I run this I don't see the blue browser frame and I don't see the window size printed when I resize the window. In fact I only see the window size printed once, when the window is created.
Output:
1 1
Look familiar?

The reason for the lack of window configure events, and the small window size, is that you have not told the windows how they are supposed to resize their respective grids. I'll add in some lines to do that.
import tkinter as tk
import sys

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

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

    def on_configure(self, event):
        print(self.winfo_width(), self.winfo_height())


def main():
    root = tk.Tk()
    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", padx=5, pady=5, bg='blue')
    home_browser.grid(row=0, column=1, sticky='news', pady=2)

    browser_frame = BrowserFrame(home_browser, bg='green')
    browser_frame.grid(row=0, column=0, sticky=('news'))

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

    root.columnconfigure(0, weight=1)
    root.columnconfigure(1, weight=5)
    root.rowconfigure(0, weight=1)

    home_browser.columnconfigure(0, weight=1)
    home_browser.rowconfigure(0, weight=1)
    root.mainloop()

if __name__ == '__main__':
    main()
Now I see the blue browser frame and I see the window size printed when I resize the window.

If you add the row/column configure to your program it should work.
Reply
#24
i see thanks :)

i still need to resize manually for the window to open and it still is not opening in the frame, i think im missing something :s


import tkinter as tk
import sys
import ctypes
import platform
from cefpython3 import cefpython as cef
from tkinter import *
import tkinter as tk
import sys
# platforms
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.parent = master
        self.bind('<Configure>', self.on_configure)
        super().__init__(master, **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 on_configure(self, event):
        if self.browser is None:
            # create the browser and embed it in current frame

            self.update()
            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)
            print(self.winfo_width(), self.winfo_height())
            # 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():
    root = tk.Tk()
    root.columnconfigure(0, weight=1)
    root.columnconfigure(1, weight=5)
    root.rowconfigure(0, weight=1)

    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", padx=5, pady=5, bg='blue')
    home_browser.grid(row=0, column=1, sticky='news', pady=2)

    browser_frame = BrowserFrame(home_browser, bg='green')
    browser_frame.grid(row=0, column=3, sticky=('news'))

    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"



    home_browser.columnconfigure(0, weight=1)
    home_browser.rowconfigure(0, weight=1)
    root.mainloop()


if __name__ == '__main__':
    main()
it still print
print(self.winfo_width(), self.winfo_height()) 
->
Output:
1 576
[Image: Captura-de-ecra-2021-10-27-a-s-09-45-40.png]
Reply
#25
(Aug-28-2021, 09:03 PM)Yoriz Wrote: cefpython has a tkinter example
https://github.com/cztomczak/cefpython/b...kinter_.py
Brooooo, can you help me? I don't know if this forum is alive or not, but I really need your help. When I try to drag a tkinter window with the "cefpython3" library, I get window "jerking" and the window does not move correctly. I took the code from the example from GitHub, which you left. I can give some information about what I "dug" in the GitHub code.
Reply
#26
(Aug-31-2021, 11:57 AM)deanhystad Wrote: 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

Solid advice here. It's always good to start with working example code before diving into integrating it into your own project. The nuances of hosting a browser window in a Tkinter app are not straightforward, so leaning on a tried-and-true example can save you a lot of headaches.

I totally agree that running the example as-is should be the first step. It'll give you a good sense of what to expect and help you decide if that's the direction you want to go for your project.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,566 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  Issue in Tkinter with winfo_class() and LabelFrame ReDefendeur 1 2,757 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