Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. # Simple sample ilustrating the usage of CEFWindow class.
  2.  
  3. # On Mac the cefpython library must be imported the very first,
  4. # before any other libraries (Issue 155).
  5. import cefpython3.wx.chromectrl as chrome
  6.  
  7. import os
  8. import wx
  9. import platform
  10.  
  11. class MyBindings:
  12.     def __init__(self, mainBrowser):
  13.         self.mainBrowser = mainBrowser
  14.    
  15.     def sendmsgnow(self, message):
  16.         print("[app-testing.py] sendmsgnow: "+message)
  17.         self.sendmsgback(message)
  18.    
  19.     def sendmsgback(self, message):
  20.         print(message)
  21.         #self.mainBrowser.GetMainFrame().ExecuteFunction("getmsg",message)
  22.         #chrome.ChromeWindow.GetBrowser.GetMainFrame().ExecuteFunction("getmsg",message)
  23.         self.cefWindow.GetBrowser.GetMainFrame().ExecuteFunction("getmsg",message)
  24.  
  25. class MainFrame(wx.Frame):
  26.     def __init__(self):
  27.         wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
  28.                           title='Test Window', size=(800,600))
  29.  
  30.         self.cefWindow = chrome.ChromeWindow(self,
  31.                 url=os.path.join(os.path.dirname(os.path.abspath(__file__)),
  32.                                  "testapp.html"))
  33.  
  34.         sizer = wx.BoxSizer()
  35.         sizer.Add(self.cefWindow, 1, wx.EXPAND, 0)
  36.         self.SetSizer(sizer)
  37.        
  38.         bindings = chrome.cefpython.JavascriptBindings(bindToFrames=False, bindToPopups=True)
  39.         self.MyBindingsObject = MyBindings(self.cefWindow)
  40.         bindings.SetObject("external", self.MyBindingsObject)
  41.         self.cefWindow.browser.SetJavascriptBindings(bindings)
  42.  
  43.         self.Bind(wx.EVT_CLOSE, self.OnClose)
  44.  
  45.     def OnClose(self, event):
  46.         # Remember to destroy all CEF browser references before calling
  47.         # Destroy(), so that browser closes cleanly. In this specific
  48.         # example there are no references kept, but keep this in mind
  49.         # for the future.
  50.         self.Destroy()
  51.         # On Mac the code after app.MainLoop() never executes, so
  52.         # need to call CEF shutdown here.
  53.         if platform.system() == "Darwin":
  54.             chrome.Shutdown()
  55.             wx.GetApp().Exit()
  56.  
  57. class MyApp(wx.App):
  58.     def OnInit(self):
  59.         frame = MainFrame()
  60.         self.SetTopWindow(frame)
  61.         frame.Show()
  62.         return True
  63.  
  64. if __name__ == '__main__':
  65.     chrome.Initialize({
  66.         "debug": True,
  67.         "log_file": "debug.log",
  68.         "log_severity": chrome.cefpython.LOGSEVERITY_INFO,
  69.         "release_dcheck_enabled": True,
  70.         # "cache_path": "webcache/",
  71.     })
  72.     print('[app-custom.py] wx.version=%s' % wx.version())
  73.     app = MyApp(False)
  74.     app.MainLoop()
  75.     # Important: do the wx cleanup before calling Shutdown
  76.     del app
  77.     # On Mac Shutdown is called in OnClose
  78.     if platform.system() in ["Linux", "Windows"]:
  79.         chrome.Shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement