def __init__(self): wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title='wxPython example') self.ShowFullScreen(True) self.browser = None # Must ignore X11 errors like 'BadWindow' and others by # installing X11 error handlers. This must be done after # wx was intialized. if LINUX: WindowUtils.InstallX11ErrorHandlers() global g_count_windows g_count_windows += 1 self.setup_icon() self.create_menu() self.Bind(wx.EVT_CLOSE, self.OnClose) # Set wx.WANTS_CHARS style for the keyboard to work. # This style also needs to be set for all parent controls. self.browser_panel = wx.Panel(self, style=wx.WANTS_CHARS) self.browser_panel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) self.browser_panel.Bind(wx.EVT_SIZE, self.OnSize) if MAC: try: # noinspection PyUnresolvedReferences from AppKit import NSApp # Make the content view for the window have a layer. # This will make all sub-views have layers. This is # necessary to ensure correct layer ordering of all # child views and their layers. This fixes Window # glitchiness during initial loading on Mac (Issue #371). NSApp.windows()[0].contentView().setWantsLayer_(True) except ImportError: print( "[wxpython.py] Warning: PyObjC package is missing, " "cannot fix Issue #371") print( "[wxpython.py] To install PyObjC type: " "pip install -U pyobjc") if LINUX: # On Linux must show before embedding browser, so that handle # is available (Issue #347). self.Show() # In wxPython 3.0 and wxPython 4.0 on Linux handle is # still not yet available, so must delay embedding browser # (Issue #349). if wx.version().startswith("3.") or wx.version().startswith("4."): wx.CallLater(100, self.embed_browser) else: # This works fine in wxPython 2.8 on Linux self.embed_browser() else: self.embed_browser() self.Show()
def __init__(self, url, icon, title, size): self.browser = None # Must ignore X11 errors like 'BadWindow' and others by # installing X11 error handlers. This must be done after # wx was intialized. if LINUX: cef.WindowUtils.InstallX11ErrorHandlers() global g_count_windows g_count_windows += 1 if WINDOWS: # noinspection PyUnresolvedReferences, PyArgumentList logging.debug("[wxpython.py] System DPI settings: %s" % str(cef.DpiAware.GetSystemDpi())) if hasattr(wx, "GetDisplayPPI"): logging.debug("[wxpython.py] wx.GetDisplayPPI = %s" % wx.GetDisplayPPI()) logging.debug("[wxpython.py] wx.GetDisplaySize = %s" % wx.GetDisplaySize()) logging.debug("[wxpython.py] MainFrame DPI scaled size: %s" % str(size)) wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title=title) # wxPython will set a smaller size when it is bigger # than desktop size. logging.debug("[wxpython.py] MainFrame actual size: %s" % self.GetSize()) ic = wx.Icon(icon, wx.BITMAP_TYPE_ICO) self.SetIcon(ic) self.Bind(wx.EVT_CLOSE, self.OnClose) # Set wx.WANTS_CHARS style for the keyboard to work. # This style also needs to be set for all parent controls. self.browser_panel = wx.Panel(self, size=tuple(size)) self.browser_panel.Bind(wx.EVT_SIZE, self.OnSize) wx.Window.Fit(self) if MAC: # Make the content view for the window have a layer. # This will make all sub-views have layers. This is # necessary to ensure correct layer ordering of all # child views and their layers. This fixes Window # glitchiness during initial loading on Mac (Issue #371). NSApp.windows()[0].contentView().setWantsLayer_(True) if LINUX: self.Show() self.embed_browser(url) else: self.embed_browser(url) self.Show()
def __init__(self): wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title='wxPython example', size=(WIDTH, HEIGHT)) self.browser = None # Must ignore X11 errors like 'BadWindow' and others by # installing X11 error handlers. This must be done after # wx was intialized. if LINUX: WindowUtils.InstallX11ErrorHandlers() global g_count_windows g_count_windows += 1 self.setup_icon() self.create_menu() self.Bind(wx.EVT_CLOSE, self.OnClose) # Set wx.WANTS_CHARS style for the keyboard to work. # This style also needs to be set for all parent controls. self.browser_panel = wx.Panel(self, style=wx.WANTS_CHARS) self.browser_panel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) self.browser_panel.Bind(wx.EVT_SIZE, self.OnSize) if MAC: try: # noinspection PyUnresolvedReferences from AppKit import NSApp # Make the content view for the window have a layer. # This will make all sub-views have layers. This is # necessary to ensure correct layer ordering of all # child views and their layers. This fixes Window # glitchiness during initial loading on Mac (Issue #371). NSApp.windows()[0].contentView().setWantsLayer_(True) except ImportError: print("[wxpython.py] Warning: PyObjC package is missing, " "cannot fix Issue #371") print("[wxpython.py] To install PyObjC type: " "pip install -U pyobjc") if LINUX: # On Linux must show before embedding browser, so that handle # is available (Issue #347). self.Show() # In wxPython 3.0 and wxPython 4.0 on Linux handle is # still not yet available, so must delay embedding browser # (Issue #349). if wx.version().startswith("3.") or wx.version().startswith("4."): wx.CallLater(1000, self.embed_browser) else: # This works fine in wxPython 2.8 on Linux self.embed_browser() else: self.embed_browser() self.Show()
def get_window_handle(self): if MAC: # Do not use self.winfo_id() on Mac, because of these issues: # 1. Window id sometimes has an invalid negative value (Issue #308). # 2. Even with valid window id it crashes during the call to NSView.setAutoresizingMask: # https://github.com/cztomczak/cefpython/issues/309#issuecomment-661094466 # # To fix it using PyObjC package to obtain window handle. If you change structure of windows then you # need to do modifications here as well. # # There is still one issue with this solution. Sometimes there is more than one window, for example when application # didn't close cleanly last time Python displays an NSAlert window asking whether to Reopen that window. In such # case app will crash and you will see in console: # > Fatal Python error: PyEval_RestoreThread: NULL tstate # > zsh: abort python tkinter_.py # Error messages related to this: https://github.com/cztomczak/cefpython/issues/441 # # There is yet another issue that might be related as well: # https://github.com/cztomczak/cefpython/issues/583 # noinspection PyUnresolvedReferences from AppKit import NSApp # noinspection PyUnresolvedReferences import objc logger.info("winfo_id={}".format(self.winfo_id())) # noinspection PyUnresolvedReferences content_view = objc.pyobjc_id(NSApp.windows()[-1].contentView()) logger.info("content_view={}".format(content_view)) return content_view elif self.winfo_id() > 0: return self.winfo_id() else: raise Exception("Couldn't obtain window handle")
def get_window_handle(self): if self.winfo_id() > 0: return self.winfo_id() elif platform.system() == "Darwin": from AppKit import NSApp import objc return objc.pyobjc_id(NSApp.windows()[-1].contentView()) else: raise Exception("Couldn't obtain window handle")
def toggle_panels(): '''Show/hide all floating windows in the current workspace.''' # get panels windows = NSApp.windows() panels = [ window for window in windows if isinstance(window, NSPanel) ] # get state show_panels = getExtensionDefault('com.hipertipo.showHidePanels', fallback=True) # hide panels if show_panels: for panel in panels: panel.orderOut_(None) setExtensionDefault('com.hipertipo.showHidePanels', False) # show panels if show_panels is False: for panel in panels: if str(type(panel)) != '<objective-c class NSColorPanel at 0x7fff750fad60>': panel.orderBack_(None) setExtensionDefault('com.hipertipo.showHidePanels', True)
def get_window_handle(self): if self.winfo_id() > 0: return self.winfo_id() elif MAC: # On Mac window id is an invalid negative value (Issue #308). # This is kind of a dirty hack to get window handle using # PyObjC package. If you change structure of windows then you # need to do modifications here as well. # noinspection PyUnresolvedReferences from AppKit import NSApp # noinspection PyUnresolvedReferences import objc # Sometimes there is more than one window, when application # didn't close cleanly last time Python displays an NSAlert # window asking whether to Reopen that window. # noinspection PyUnresolvedReferences return objc.pyobjc_id(NSApp.windows()[-1].contentView()) else: raise Exception("Couldn't obtain window handle")
def toggle_panels(): """Show/hide all floating windows in the current workspace.""" # get panels windows = NSApp.windows() panels = [window for window in windows if isinstance(window, NSPanel)] # get state show_panels = getExtensionDefault('com.hipertipo.showHidePanels', fallback=True) # hide panels if show_panels: for panel in panels: panel.orderOut_(None) setExtensionDefault('com.hipertipo.showHidePanels', False) # show panels if show_panels is False: for panel in panels: if str( type(panel) ) != '<objective-c class NSColorPanel at 0x7fff750fad60>': panel.orderBack_(None) setExtensionDefault('com.hipertipo.showHidePanels', True)
def __init__(self): self.browser = None # Must ignore X11 errors like 'BadWindow' and others by # installing X11 error handlers. This must be done after # wx was intialized. if LINUX: cef.WindowUtils.InstallX11ErrorHandlers() global g_count_windows g_count_windows += 1 if WINDOWS: # noinspection PyUnresolvedReferences, PyArgumentList print("[wxpython.py] System DPI settings: %s" % str(cef.DpiAware.GetSystemDpi())) if hasattr(wx, "GetDisplayPPI"): print("[wxpython.py] wx.GetDisplayPPI = %s" % wx.GetDisplayPPI()) print("[wxpython.py] wx.GetDisplaySize = %s" % wx.GetDisplaySize()) print("[wxpython.py] MainFrame declared size: %s" % str( (WIDTH, HEIGHT))) size = scale_window_size_for_high_dpi(WIDTH, HEIGHT) print("[wxpython.py] MainFrame DPI scaled size: %s" % str(size)) wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title='wxPython example', size=size) # wxPython will set a smaller size when it is bigger # than desktop size. print("[wxpython.py] MainFrame actual size: %s" % self.GetSize()) self.setup_icon() self.create_menu() self.Bind(wx.EVT_CLOSE, self.OnClose) # Set wx.WANTS_CHARS style for the keyboard to work. # This style also needs to be set for all parent controls. self.browser_panel = wx.Panel(self, style=wx.WANTS_CHARS) self.browser_panel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) self.browser_panel.Bind(wx.EVT_SIZE, self.OnSize) if MAC: # Make the content view for the window have a layer. # This will make all sub-views have layers. This is # necessary to ensure correct layer ordering of all # child views and their layers. This fixes Window # glitchiness during initial loading on Mac (Issue #371). NSApp.windows()[0].contentView().setWantsLayer_(True) if LINUX: # On Linux must show before embedding browser, so that handle # is available (Issue #347). self.Show() # In wxPython 3.0 and wxPython 4.0 on Linux handle is # still not yet available, so must delay embedding browser # (Issue #349). if wx.version().startswith("3.") or wx.version().startswith("4."): wx.CallLater(100, self.embed_browser) else: # This works fine in wxPython 2.8 on Linux self.embed_browser() else: self.embed_browser() self.Show()
# [h] close all panels from AppKit import NSApp, NSPanel windows = NSApp.windows() panels = [window for window in windows if isinstance(window, NSPanel)] for panel in panels: panel.performClose_(None)