1+ import tkinter
2+ import tkinter .ttk
3+ import platform
4+ from cefpython3 import cefpython as cef
5+ import ctypes
6+ import os
7+ # Platforms
8+ WINDOWS = (platform .system () == "Windows" )
9+ LINUX = (platform .system () == "Linux" )
10+ MAC = (platform .system () == "Darwin" )
11+ class BrowserFrame (tkinter .ttk .Frame ):
12+ def __init__ (self , master , navigation_bar = None ):
13+ cef .Initialize ()
14+ self .navigation_bar = navigation_bar
15+ self .closing = False
16+ self .browser = None
17+ self .URL = "http://www.baidu.com"
18+ tkinter .ttk .Frame .__init__ (self , master )
19+ # self.bind("<FocusIn>", self.on_focus_in)
20+ # self.bind("<FocusOut>", self.on_focus_out)
21+ self .bind ("<Configure>" , self .on_configure )
22+ # self.focus_set()
23+ print ("BrowserFrame" )
24+ def setURL (self ,url ):
25+ self .URL = url
26+ if self .browser :
27+ self .browser .LoadUrl (self .URL )
28+ print ("setURL:" + self .URL )
29+ def embed_browser (self ,width ,heith ):
30+ window_info = cef .WindowInfo ()
31+ rect = [0 , 0 , width , heith ]
32+ window_info .SetAsChild (self .get_window_handle (), rect )
33+ self .browser = cef .CreateBrowserSync (window_info ,url = self .URL )
34+ assert self .browser
35+ self .message_loop_work ()
36+ def get_window_handle (self ):
37+ if self .winfo_id () > 0 :
38+ return self .winfo_id ()
39+ elif MAC :
40+ # On Mac window id is an invalid negative value (Issue #308).
41+ # This is kind of a dirty hack to get window handle using
42+ # PyObjC package. If you change structure of windows then you
43+ # need to do modifications here as well.
44+ # noinspection PyUnresolvedReferences
45+ from AppKit import NSApp
46+ # noinspection PyUnresolvedReferences
47+ import objc
48+ # Sometimes there is more than one window, when application
49+ # didn't close cleanly last time Python displays an NSAlert
50+ # window asking whether to Reopen that window.
51+ # noinspection PyUnresolvedReferences
52+ return objc .pyobjc_id (NSApp .windows ()[- 1 ].contentView ())
53+ else :
54+ raise Exception ("Couldn't obtain window handle" )
55+ def message_loop_work (self ):
56+ cef .MessageLoopWork ()
57+ os .system ("" )
58+ self .after (10 , self .message_loop_work )
59+ def on_configure (self ,event ):
60+ print ("on_configure" )
61+ if not self .browser :
62+ print ("embed_browser" )
63+ self .embed_browser (event .width ,event .height )
64+ else :
65+ print ("on_mainframe_configure" )
66+ self .on_mainframe_configure (event .width ,event .height )
67+ def on_root_configure (self ):
68+ # Root <Configure> event will be called when top window is moved
69+ if self .browser :
70+ self .browser .NotifyMoveOrResizeStarted ()
71+ def on_mainframe_configure (self , width , height ):
72+ if self .browser :
73+ if WINDOWS :
74+ ctypes .windll .user32 .SetWindowPos (
75+ self .browser .GetWindowHandle (), 0 ,
76+ 0 , 0 , width , height , 0x0002 )
77+ elif LINUX :
78+ self .browser .SetBounds (0 , 0 , width , height )
79+ self .browser .NotifyMoveOrResizeStarted ()
80+ def on_focus_in (self , _ ):
81+ if self .browser :
82+ self .browser .SetFocus (True )
83+ def on_focus_out (self , _ ):
84+ if self .browser :
85+ self .browser .SetFocus (False )
86+ def on_root_close (self ):
87+ if self .browser :
88+ self .browser .CloseBrowser (True )
89+ self .clear_browser_references ()
90+ self .destroy ()
91+ cef .Shutdown ()
92+ def clear_browser_references (self ):
93+ # Clear browser references that you keep anywhere in your
94+ # code. All references must be cleared for CEF to shutdown cleanly.
95+ self .browser = None
0 commit comments