Posts: 6,826
Threads: 20
Joined: Feb 2020
I think this may be a tkinter layout problem. What is the location and size of the BrowserFrame?
Posts: 18
Threads: 2
Joined: Aug 2021
(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>
Posts: 6,826
Threads: 20
Joined: Feb 2020
I removed all the cef stuff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
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.
Posts: 18
Threads: 2
Joined: Aug 2021
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
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
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 :
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())
self .cef_loop()
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
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
home_browser.columnconfigure( 0 , weight = 1 )
home_browser.rowconfigure( 0 , weight = 1 )
root.mainloop()
if __name__ = = '__main__' :
main()
|
it still print
1 |
print ( self .winfo_width(), self .winfo_height())
|
-> Output: 1 576
Posts: 1
Threads: 0
Joined: Aug 2023
Aug-28-2023, 07:33 PM
(This post was last modified: Aug-28-2023, 07:34 PM by wwenttyy.)
(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.
Posts: 2
Threads: 0
Joined: Sep 2023
Sep-15-2023, 01:38 PM
(This post was last modified: Sep-19-2023, 04:12 AM by Larz60+.
Edit Reason: deleted spam link
)
(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.
1 2 3 4 5 |
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).
1 2 3 |
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.
1 2 3 4 5 6 7 8 9 |
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.
|