How to get Gdk window from xid? - python-3.x

I'm lost in version 3.. in python2+gdk2 is:
#!/usr/bin/env python2
import gtk
gtk.gdk.window_process_all_updates()
window_xid = 54525964
gdk_window = gtk.gdk.window_foreign_new(window_xid)
which is pretty much straight forward. But then, the horror:
#!/usr/bin/env python3
from gi.repository import xlib
from gi.repository import Gdk
from gi.repository import GdkX11
Gdk.Window.process_all_updates()
xlib_window = "???????"
gdk_display = GdkX11.X11Display.get_default()
gdk_window = GdkX11.X11Window.foreign_new_for_display(gdk_display, xlib_window)
the xlib is killing me.. I'm unable to do anything with it. Has anybody worked with it before??
The documentation I've through several times already is:
Gdk3,
Xlib
Getting the window from its xid was the fastest way to get a screenshot in python2 I guess I'll have try another way in python3.. any ideas? maybe peek_children from root window?? but then, how do I know if it's the window I want?

A Window in X11 is the same as an XID. There's just a typedef from one to the other.
So in C code gdk_x11_window_foreign_new_for_display() just accepts an Window or XID, which is basically an integer. This also works in python using introspection:
#!/usr/bin/env python3
from gi.repository import Gdk
from gi.repository import GdkX11
Gdk.Window.process_all_updates()
xlib_window = 0x2a00005 # from xwininfo command
gdk_display = GdkX11.X11Display.get_default()
gdk_window = GdkX11.X11Window.foreign_new_for_display(gdk_display, xlib_window)
print(gdk_window.get_geometry())

Related

How to reliably convince matplotlib to use PySide2 backend when running from Anaconda (Spyder)

I am creating a PySide2 application which uses matplotlib. I am running this application from Spyder in an environment with PySide2 installed. This is causing the application to be run from the iPython console. Somewhere along the line, PyQt5 is imported, which I am attempting to purge in order to convince matplotlib that I really do want to use PySide2, NOT PyQt5. Something like following was working until very recently and I am not really sure why it has stopped, but safe to say this method is unreliable. How can I absolutely convince matplotlib that I am wanting PySide2?
I have tried setting the environment variable QT_API in the operating system (Windows 10), but in this case Spyder itself refuses to open.
import sys
import os
ps = list(filter(lambda x: 'PyQt5' in x, sys.modules))
for p in ps:
print(f"purging module {p}")
sys.modules.pop(p)
# matplotlib.__init__ uses this
os.environ["MPLBACKEND"] = "PySide2"
# matplotlib.backends.qt_compat uses this
os.environ["QT_API"] = "PySide2"
import PySide2.QtCore
assert "PyQt5.QtCore" not in sys.modules
assert "PySide2.QtCore" in sys.modules
# rcParams has the right idea
from matplotlib import rcParams
print(rcParams["backend"])
# qt_compat has the WRONG idea!
import matplotlib.backends.qt_compat as qt_compat
print(qt_compat.QT_API)
# The FigureCanvasWidget is of the wrong (PyQt5) type
from matplotlib.backends.backend_qt5agg import FigureCanvas
import inspect
print(inspect.getmro(FigureCanvas))
To answer this question, the reason that it stopped working was because I had set 'activate support' for Matplotlib graphics in the ipython tab under Spyder settings. After unchecking this, the above works.

Many errors with gi.repository.Gtk in Python3

I just upgraded from Fedora 30 to Fedora 32. All my python stuff stopped working.
The first problem was with
import gtk
I read that I should switch it to
from gi.repository import Gtk as gtk
from gi.repository import Gdk
But many many modules are not present. For example:
AttributeError: 'gi.repository.Gtk' object has no attribute 'combo_box_new_text'
AttributeError: 'gi.repository.Gtk' object has no attribute 'WINDOW_TOPLEVEL'
AttributeError: 'gi.repository.Gtk' object has no attribute 'settings_get_for_screen'
A huge mess.
How do I fix this?
Using Gtk 3:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
in gtk use:
self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
in Gtk 3 use:
self.set_position(Gtk.WindowPosition.CENTER)
So must convert your code to version 3 .

Pyinstaller with anaconda in spyder

So i wrote a little programm in python with use of tkinter. The repo is found here: https://github.com/Odatas/MeisterTools
I now want to create an exe so people only need to use the exe when they want to use the program. But the exe i create with pyinstaller doesnt work and throws the error:
Import Error: cannot import name 'travel' from 'main'
The cmd i opend to creat the exe is directly out of anaconda envoirment.
I cd into the folder where all the scripts are and then run it like this:
pyinstaller --onefile patrickstools2.py
I even tried to make every import an hidden import:
pyinstaller --onefile --hidden-import=init --hidden-import=main --hidden-import=checker --hidden-import=contact --hidden-import=dangers --hidden-import=droptable --hidden-import=importexcel --hidden-import=odatasfunctions --hidden-import=randomenpc --hidden-import=scrolltest --hidden-import=sonstiges --hidden-import=travel patrickstools2.py
that doesnt help either. I added the path through Anaconda to the PYTHONPATH variable...so it should be know in any way shape or form.
The complet code is in Anaconda. The Error gets thrown in the import section of the main file:
# page classes import
import os
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
# page classes import
from . import travel
from . import contact
from . import dangers
from . import sonstiges
from . import randomenpc
I allready created an exe with pyinstaller from a previous version. But i made some changes to the structure of the programm. The run.py is only there because i work with spyder and as far as i know spyder needs it because else the import doesnt work correctly.

Python - importing module with Glib mainloop

I'm using pygattlib to interface with Bluetooth device in Python.
It works just fine, but the problem occurs when I try to use DBus in my code.
If I import gattlib and GLib.MainLoop().run(), the program freezes when calling run().
I found that gattlib has its own MainLoop for internal async calls.
I didn't find anything that would cover this specific issue, just some examples of having multiple mainloops in C.
This is the relevant part of the Python code:
import gattlib as bt
try:
from gi.repository import GLib
except ImportError:
import glib as GLib
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
[...do stuff with gattlib here...]
DBusGMainLoop(set_as_default=True)
dbusService = SystemDBus()
try:
loop = GLib.MainLoop()
loop.run() # it stalls here, doesn't respond to DBus or anything
except KeyboardInterrupt:
GLib.MainLoop().quit()

Error "Could not find any typelib for Gtk" with Python3 and GTK3

I cannot make Python3 work with GTK3. I'm in a cluster context and I had everything recompiled from the sources.
When I run a simple example :
from gi.repository import Gtk
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
I have the following error :
ERROR:root:Could not find any typelib for Gtk
Traceback (most recent call last):
File "gtk3_example.py", line 2, in
from gi.repository import Gtk
ImportError: cannot import name 'Gtk'
There are typically additional packages to install depending on what you want to introspect. The one I found that was crucial was gir1.2-gtk-3.0 (or 2.0 depending which version you are coding against).
The problem is due to the compiled version of GTK3 that was not referenced in gobject-introspection.
It is simple to check the problem by listing the .typelib files in gobject-introspection
$ which g-ir-scanner
/Produits/publics/x86_64.Linux.RH6/gobject-introspection/1.40.0/bin/g-ir-scanner
$ ls /Produits/publics/x86_64.Linux.RH6/gobject-introspection/1.40.0/lib/girepository-1.0/
cairo-1.0.typelib fontconfig-2.0.typelib GIRepository-2.0.typelib GModule-2.0.typelib win32-1.0.typelib xlib-2.0.typelib
DBus-1.0.typelib freetype2-2.0.typelib GL-1.0.typelib GObject-2.0.typelib xfixes-4.0.typelib xrandr-1.3.typelib
DBusGLib-1.0.typelib Gio-2.0.typelib GLib-2.0.typelib libxml2-2.0.typelib xft-2.0.typelib
The problem should be solved by recompiling GTK (and its dependencies) with the configure option --enable-introspection=yes.
Try using the following code:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GTK

Resources