Discover what window is active on Gnome/Linux/Ubuntu from Python? - python

Is there any way to get a list of all windows that are open at present and see what window is at the top (i.e. active?) from Python?
This is using Gnome on Ubuntu Linux.
wnck looks like it might do this, but it's very lacking in documentation.

Here's the same code using the modern GObject Introspection libraries instead of the now deprecated PyGTK method Josh Lee posted:
from gi.repository import Gtk, Wnck
Gtk.init([]) # necessary if not using a Gtk.main() loop
screen = Wnck.Screen.get_default()
screen.force_update() # recommended per Wnck documentation
window_list = screen.get_windows()
active_window = screen.get_active_window()
As for documentation, check out the Libwnck Reference Manual. It is not specific for python, but the whole point of using GObject Introspection is to have the same API across all languages, thanks to the gir bindings.
Also, Ubuntu ships with both wnck and its corresponding gir binding out of the box, but if you need to install them:
sudo apt-get install libwnck-3-* gir1.2-wnck-3.0
This will also install libwnck-3-dev, which is not necessary but will install useful documentation you can read using DevHelp

import wnck
screen = wnck.screen_get_default()
window_list = screen.get_windows()
active_window = screen.get_active_window()
See also Get active window title in X, and WnckScreen in the documentation. Other questions containing wnck have useful code samples.

Related

How to get windows 10 device specifications pen and touch properties with python?

I would like to get windows 10 device specifications pen and touch properties with python
I try to use two ways :
systeminfo in command line
msinfo32
Both ways did not find the answer,Is there any way to get this?
I expect that there can be Python Package or other methods
you can use pyglet module to get all devices connected to your pc/laptop
first install pyglet pip install pyglet
code
import pyglet
devices = pyglet.input.get_devices()
print(devices)
reference question
here
pyglet docs

Making Matplotlib and GTK3 work on python3 windows

I'm trying to make GTK3 and Python3 work under windows to my project.
I have an continuum anaconda setup with a 32-bit python 3.4 and Matplotib via
conda install matplotlib.
I've installed PyGobject(https://sourceforge.net/projects/pygobjectwin32/) and installed GTK+ / Glade via the installer.
The basic exemple from the GTK3 tutorial works well (empty screen)
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
I want now to embed matplotlib in gtk, I got the example from matplotlib (http://matplotlib.org/examples/user_interfaces/embedding_in_gtk3.html)
I saw then that I needed cairocffi because some incompabilities. (PyCairo has no support for a matplotlib function)
I got the windows binaries for cffi from Gohlke
And finnaly did a
pip install cairocffi
And now I just get a python.exe stopped working.
Tried with GTK3agg and GTK3Cairo backends and I have the same result
Looking around I found that maybe the cairo version is outdated for the functions used by matplotlib, but I dont know how to proceed.
Cairocffi works if I try running something else.
More information (from the comment below):
I still got an unhandled win32 error. I managed to open the error and it says:
Unhandled exception at 0x08CF6D58 (libcairo-2.dll) in python.exe:
0xC0000005: Access violation reading location 0x000000A8.
If there is a handler for this exception, the program may be safely continued.
It just crashes...
I've had my share of problems using matplotlib in Python3 + Gtk3. I found this cookbook page with working examples. Try to run the examples in the cookbook - particularly the simplest one:
#!/usr/bin/python3
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import arange, pi, random, linspace
import matplotlib.cm as cm
#Possibly this rendering backend is broken currently
#from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
myfirstwindow = Gtk.Window()
myfirstwindow.connect("delete-event", Gtk.main_quit)
myfirstwindow.set_default_size(400, 400)
fig = Figure(figsize=(5,5), dpi=100)
ax = fig.add_subplot(111, projection='polar')
N = 20
theta = linspace(0.0, 2 * pi, N, endpoint=False)
radii = 10 * random.rand(N)
width = pi / 4 * random.rand(N)
bars = ax.bar(theta, radii, width=width, bottom=0.0)
for r, bar in zip(radii, bars):
bar.set_facecolor(cm.jet(r / 10.))
bar.set_alpha(0.5)
ax.plot()
sw = Gtk.ScrolledWindow()
myfirstwindow.add(sw)
canvas = FigureCanvas(fig)
canvas.set_size_request(400,400)
sw.add_with_viewport(canvas)
myfirstwindow.show_all()
Gtk.main()
Also, not that you need a fairly recent version of matplotlib to make things work on Python3.
If you still have problems, please show us the complete error message.
Note: I tested this on Linux (don't have Windows), but, from you description of problems, the issue is (was) a common one.
I have the same issue on Windows since years. The documentation of matplotlib 2.0.0 realease states that Gtk3 backend is not supported on Windows. Recently, I had an issue running the Gtk3Agg backend under Linux (Ubuntu). In both cases, it is always related to Cairo.
Thus, I wrote my own implementation of this backend, you can find it here. import the FigureCanvasGtk3Agg from my module and use it the same way as the official one. It lacks some features, but, if you just want to display a plot, it'll do the job.
You can try the module by running it, it should display a simple colorful graph in a window. I tried it under both Linux and Windows and had no issue.
How does it works:
The trick is to avoid importing Cairo in Python3 as it usually doesn't work (to my experience). It is done by using a GdkPixbuf.
Then Gdk.cairo_set_source_pixbuf does the rest of the job along with calling two methods of the cairo context provided by Gtk in the 'draw-event' callback. Cairo is never imported in the module.

Error: no module named gtk.glade

I researched and tried this for two days now and I cannot get gtk to work on Windows 7 with Python 3.4! Whenever I launch my .py file on Python 3.4 with import gtk, I get No module named gtk! I installed pygobject but it did not help. Even gtk3-demo command works in the windows cmd prompt.
I finally got gtk to import (I think) by copying the GTK directory right to C:\Python34\Lib. But now I have a problem with gtk.glade.
Where is this? Where do I copy it from and to where?
You are possibly using an outdated tutorial, see the current Python GTK 3 tutorial for a more up-to-date reference.
In particular, the way to import GTK has changed in GTK3 to:
from gi.repository import Gtk
And instead of libglade, you would use the newer Gtk.Builder class like so:
ui = Gtk.Builder()
ui.add_from_file("my_glade_file.glade")
(you still develop the UI using Glade, it is only how you access it from your program that has changed).

Apply GTK theme for Python window Ubuntu 14.04

I am rather new to Python and I don't seem to find any solution of changing the GTK theme that functions on my system. I run Ubuntu 14.04 with Python 2.7.6. The following is a simplified version of the code I'm currently working on:
import gtk
import webkit
gtk.rc_parse('/home/viktor/.themes/Elegant Brit/gtk-2.0/gtkrc')
view = webkit.WebView()
sw = gtk.ScrolledWindow()
sw.add(view)
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.add(sw)
win.show_all()
view.open("http://w3.org/")
gtk.main()
I can't seem to find anything that works for me. Any help is appreciated.
the theme can be modified using gtkrc files stored in the following path:
/usr/share/themes/[YOUR THEME]/[GTK VERSION]/gtkrc
use the following code to set the theme manually in your program
import os
os.environ['GTK2_RC_FILES'] = '/PATH/TO/THEME/gtkrc'
and walla! Your window will now use the specified theme.
Tested this using wxPython 2.8.

Trouble with simple gtk python script

I am new to python and am trying to run a simple program I found on the web. I have installed the pyGTK All In One Download and cannot (after searching on the web for a month or so) find gtk.py anywhere.
It does not exist on my computer no matter how many times i run pygtk-all-in-one-2.24.2.win32-py2.7.msi
Here is my code
import pygtk
pygtk.require('2.0')
import gtk
class Simple:
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.connect("destroy", self.destroy)
win.set_title("Simple")
win.set_position(gtk.WIN_POS_CENTER)
win.show_all()
Simple()
gtk.main()
I searched this forum and many others as well. If you have this file, could you post the code so I can copy it?
If someone can help I would be very grateful. Thank You for listening.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
It seems that I have found the answer from the following link at http://www.slideshare.net/BaabtraMentoringPartner/importerror-dll-load-failed-is-not-a-valid-win32-application-python-mysql. I uninstalled the 64 bit version and installed the 32 bit version. When I run it from the command line the little window comes up! The error still shows up in Netbeans though so maybe the problem is in the Netbeans IDE?
The gtk code is in
C:\Python27\Lib\site-packages\gtk-2.0
There is no single file named gtk.py. (There is a _gtk.pyd, which is like a dll).
However, you should generally not need to access the source code for the libraries you install. Python will handle that for you when you import gtk, then you can just use those features within your script.
If you have another gtk app (like GIMP) you may have problem with enviroment. Also, I hope that the indentation is broken only here, not in your script. And tell us what is the error.
I guess that using python(64bit ) for Windows 7 was the main problem.
So I uninstalled the 64 bit version and installed the 32 bit version and it seems to work fine now.
Thanks to everyone for your help.
I would like to mark this question as answered but I don't exactly know how.
Uninstall your all-in-one and try with this other all-in-one:
https://sourceforge.net/projects/pygobjectwin32/files/
Then:
import gi
gi.require_version('Gtk', '2.0')
from gi.repository import Gtk

Categories