| 152 | |
| 153 | |
| 154 | class BrowserFrame(tk.Frame): |
| 155 | |
| 156 | def __init__(self, mainframe, navigation_bar=None): |
| 157 | self.navigation_bar = navigation_bar |
| 158 | self.closing = False |
| 159 | self.browser = None |
| 160 | tk.Frame.__init__(self, mainframe) |
| 161 | self.mainframe = mainframe |
| 162 | self.bind("<FocusIn>", self.on_focus_in) |
| 163 | self.bind("<FocusOut>", self.on_focus_out) |
| 164 | self.bind("<Configure>", self.on_configure) |
| 165 | """For focus problems see Issue #255 and Issue #535. """ |
| 166 | self.focus_set() |
| 167 | |
| 168 | def embed_browser(self): |
| 169 | window_info = cef.WindowInfo() |
| 170 | rect = [0, 0, self.winfo_width(), self.winfo_height()] |
| 171 | window_info.SetAsChild(self.get_window_handle(), rect) |
| 172 | self.browser = cef.CreateBrowserSync(window_info, |
| 173 | url="https://www.google.com/") |
| 174 | assert self.browser |
| 175 | self.browser.SetClientHandler(LifespanHandler(self)) |
| 176 | self.browser.SetClientHandler(LoadHandler(self)) |
| 177 | self.browser.SetClientHandler(FocusHandler(self)) |
| 178 | self.message_loop_work() |
| 179 | |
| 180 | def get_window_handle(self): |
| 181 | if MAC: |
| 182 | # Do not use self.winfo_id() on Mac, because of these issues: |
| 183 | # 1. Window id sometimes has an invalid negative value (Issue #308). |
| 184 | # 2. Even with valid window id it crashes during the call to NSView.setAutoresizingMask: |
| 185 | # https://github.com/cztomczak/cefpython/issues/309#issuecomment-661094466 |
| 186 | # |
| 187 | # To fix it using PyObjC package to obtain window handle. If you change structure of windows then you |
| 188 | # need to do modifications here as well. |
| 189 | # |
| 190 | # There is still one issue with this solution. Sometimes there is more than one window, for example when application |
| 191 | # didn't close cleanly last time Python displays an NSAlert window asking whether to Reopen that window. In such |
| 192 | # case app will crash and you will see in console: |
| 193 | # > Fatal Python error: PyEval_RestoreThread: NULL tstate |
| 194 | # > zsh: abort python tkinter_.py |
| 195 | # Error messages related to this: https://github.com/cztomczak/cefpython/issues/441 |
| 196 | # |
| 197 | # There is yet another issue that might be related as well: |
| 198 | # https://github.com/cztomczak/cefpython/issues/583 |
| 199 | |
| 200 | # noinspection PyUnresolvedReferences |
| 201 | from AppKit import NSApp |
| 202 | # noinspection PyUnresolvedReferences |
| 203 | import objc |
| 204 | logger.info("winfo_id={}".format(self.winfo_id())) |
| 205 | # noinspection PyUnresolvedReferences |
| 206 | content_view = objc.pyobjc_id(NSApp.windows()[-1].contentView()) |
| 207 | logger.info("content_view={}".format(content_view)) |
| 208 | return content_view |
| 209 | elif self.winfo_id() > 0: |
| 210 | return self.winfo_id() |
| 211 | else: |