我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用gi.require_version()。
def get_gi_libdir(module, version): statement = """ import gi gi.require_version("GIRepository", "2.0") from gi.repository import GIRepository repo = GIRepository.Repository.get_default() module, version = (%r, %r) repo.require(module, version, GIRepository.RepositoryLoadFlags.IREPOSITORY_LOAD_FLAG_LAZY) print(repo.get_shared_library(module)) """ statement %= (module, version) libs = exec_statement(statement).split(',') for lib in libs: path = findSystemLibrary(lib.strip()) return os.path.normpath(os.path.dirname(path)) raise ValueError("Could not find libdir for %s-%s" % (module, version))
def main(): import gi gi.require_version('Gtk', '3.0') gi.require_version('Rsvg', '2.0') gi.require_version('GdkX11', '3.0') from scc.tools import init_logging from scc.paths import get_share_path init_logging() gd = GestureDisplay() if not gd.parse_argumets(sys.argv): sys.exit(1) gd.run() if gd.get_exit_code() == 0: print gd.get_gesture() else: sys.exit(gd.get_exit_code())
def test_packages(self): """ Tests if every known Action is documentated in docs/actions.md """ try: import gi gi.require_version('Gtk', '3.0') gi.require_version('GdkX11', '3.0') gi.require_version('Rsvg', '2.0') except ImportError: pass from setup import packages for importer, modname, ispkg in pkgutil.walk_packages(path=scc.__path__, prefix="scc.", onerror=lambda x: None): if ispkg: assert modname in packages, "Package '%s' is not being installed by setup.py" % (modname,)
def _gst_available(): """Determine whether Gstreamer and the Python GObject bindings are installed. """ try: import gi except ImportError: return False try: gi.require_version('Gst', '1.0') except (ValueError, AttributeError): return False try: from gi.repository import Gst # noqa except ImportError: return False return True
def is_quartz(): """ Tests to see if Python is currently running with gtk and windowing system is Mac OS-X's "quartz". """ if mac(): try: import gi gi.require_version('Gtk', '3.0') gi.require_version('Gdk', '3.0') from gi.repository import Gtk from gi.repository import Gdk except ImportError: return False return Gdk.Display.get_default().__class__.__name__.endswith("QuartzDisplay") return False
def backend_gtk3agg_internal_check(x): try: import gi except ImportError: return (False, "Requires pygobject to be installed.") try: gi.require_version("Gtk", "3.0") except ValueError: return (False, "Requires gtk3 development files to be installed.") except AttributeError: return (False, "pygobject version too old.") try: from gi.repository import Gtk, Gdk, GObject except (ImportError, RuntimeError): return (False, "Requires pygobject to be installed.") return (True, "version %s.%s.%s" % ( Gtk.get_major_version(), Gtk.get_micro_version(), Gtk.get_minor_version()))
def import_optional(): """Safe module import""" success = AttributeDict() try: gi.require_version('Gst', '1.0') from gi.repository import Gst # noqa: F401 success.gstreamer = True except Exception: success.gstreamer = False logger.warning("Fail to import Gstreamer module") try: from PIL import Image # noqa: F401 success.pillow = True except Exception: success.pillow = False logger.warning("Fail to import Pillow module") return success
def monitor_geometry(): "Return the current monitor geometry" import gi gi.require_version('Gdk', '3.0') from gi.repository import Gdk from collections import namedtuple MonitorGeometry = namedtuple( 'MonitorGeometry', ['left', 'right', 'top', 'bottom', 'width', 'height'], ) display = Gdk.Display.get_default() screen = display.get_default_screen() window = screen.get_active_window() monitor = screen.get_monitor_at_window(window) g = screen.get_monitor_geometry(monitor) right = g.x + g.width bottom = g.y + g.height return MonitorGeometry(g.x, right, g.y, bottom, g.width, g.height)
def import_osd(): import gi gi.require_version('Gtk', '3.0') gi.require_version('Rsvg', '2.0') gi.require_version('GdkX11', '3.0')
def cmd_dependency_check(argv0, argv): """ Checks if all required libraries are installed on this system """ try: import gi gi.require_version('Gtk', '3.0') gi.require_version('GdkX11', '3.0') gi.require_version('Rsvg', '2.0') except ValueError, e1: print >>sys.stderr, e1 if "Rsvg" in str(e1): print >>sys.stderr, "Please, install 'gir1.2-rsvg-2.0' package to use this application" else: print >>sys.stderr, "Please, install 'PyGObject' package to use this application" except ImportError, e2: print e2 return 1 try: import evdev except Exception, e: print >>sys.stderr, e print >>sys.stderr, "Please, install python-evdev package to enable non-steam controller support" try: import scc.lib.xwrappers as X X.Atom except Exception, e: print >>sys.stderr, e print >>sys.stderr, "Failed to load X11 helpers, please, check your X installation" return 1 return 0
def has_display(): """ Tests to see if Python is currently running with gtk """ # FIXME: currently, Gtk.init_check() requires all strings # in argv, and we might have unicode. temp, sys.argv = sys.argv, sys.argv[:1] try: import gi gi.require_version('Gtk', '3.0') gi.require_version('Gdk', '3.0') from gi.repository import Gtk from gi.repository import Gdk except ImportError: return False try: test = Gtk.init_check(temp) and \ Gdk.Display.get_default() sys.argv = temp return bool(test) except: sys.argv = temp return False # A couple of places add menu accelerators using <alt>, which doesn't # work with Gtk-quartz. <Meta> is the usually correct replacement, but # in one case the key is a number, and <meta>number is used by Spaces # (a mac feature), so we'll use control instead.
def gui(): """Start the graphical user interface.""" try: import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk except ImportError: print("Sorry, this needs pygobject to be installed!") sys.exit() win = Gtk.Window(title="Set display brightness") ad1 = Gtk.Adjustment(value=get_actual_brightness(), lower=11, upper=255) scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL, adjustment=ad1) def on_scale_changed(s, _): value = int(s.get_value()) set_brightness(value) scale.connect("button-release-event", on_scale_changed) scale.connect("key_release_event", on_scale_changed) scale.connect("scroll-event", on_scale_changed) scale.set_size_request(350, 50) # Main Container main_container = Gtk.Fixed() main_container.put(scale, 10, 10) # Main Window win.connect("delete-event", Gtk.main_quit) win.connect("destroy", Gtk.main_quit) win.add(main_container) win.resize(400, 50) win.set_position(Gtk.WindowPosition.CENTER) win.show_all() Gtk.main()
def test_gi_gst_binding(pyi_builder): pyi_builder.test_source(''' import gi gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) print(Gst) ''') ## For PyGObject >= 2.0 # Names of all "gi.repository" packages provided by PyGObject >= 2.0 to be # tested below, typically corresponding to those packages hooked by PyInstaller.
def test_gi2_repository(pyi_builder, repository_name): ''' Test the importability of the `gi.repository` subpackage with the passed name installed with PyGObject >= 2.0 (e.g., `GLib`, corresponding to the `gi.repository.GLib` subpackage). ''' # Test the importability of this subpackage. pyi_builder.test_source(''' import gi gi.require_version('{repository_name}', '2.0') from gi.repository import {repository_name} print({repository_name}) '''.format(repository_name=repository_name))
def get_glib_system_data_dirs(): statement = """ import gi gi.require_version('GLib', '2.0') from gi.repository import GLib print(GLib.get_system_data_dirs()) """ data_dirs = eval_statement(statement) if not data_dirs: logger.error("gi repository 'GIRepository 2.0' not found. " "Please make sure libgirepository-gir2.0 resp. " "lib64girepository-gir2.0 is installed.") # :todo: should we raise a SystemError here? return data_dirs
def __init__(self, player): self.player = player self.logger = logging.getLogger('KeyboardClient') try: import gi gi.require_version('Keybinder', '3.0') from gi.repository import Keybinder Keybinder.init() Keybinder.bind('XF86AudioPlay', self._on_XF86AudioPlay) Keybinder.bind('XF86AudioNext', self._on_XF86AudioNext) Keybinder.bind('XF86AudioPrev', self._on_XF86AudioPrev) except ValueError: self.logger.warning('Keybinder is needed on Linux for MediaKey binding')
def backend_gtk3cairo_internal_check(x): try: import cairocffi except ImportError: try: import cairo except ImportError: return (False, "Requires cairocffi or pycairo to be installed.") try: import gi except ImportError: return (False, "Requires pygobject to be installed.") try: gi.require_version("Gtk", "3.0") except ValueError: return (False, "Requires gtk3 development files to be installed.") except AttributeError: return (False, "pygobject version too old.") try: from gi.repository import Gtk, Gdk, GObject except (RuntimeError, ImportError): return (False, "Requires pygobject to be installed.") return (True, "version %s.%s.%s" % ( Gtk.get_major_version(), Gtk.get_micro_version(), Gtk.get_minor_version()))