Simple Pantheon panel applet on eOS? - python

I would like to make a simple applet for the Pantheon Panel on eOS Luna with Python. I can't find any documentation on any API. It's been suggested on some forum I should use the same procedure as Gnome or Unity. The applets I have tried, however (like the one on this answer), simply didn't work.
Could you guide me a little towards what I should be doing to have a simple applet icon + menu showing on the Pantheon panel?

It seems one has to use the App Indicator module as per Ubuntu documentation. The appindicator package of PyGtk didn't work out, but the PyGi AppIndicator3 does work fine as far as I can tell.
A simple example is:
#!/usr/env/bin/ python
from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator
def menuitem_response(w, buf):
print buf
if __name__ == "__main__":
ind = appindicator.Indicator.new (
"example-simple-client",
"indicator-messages",
appindicator.IndicatorCategory.APPLICATION_STATUS)
ind.set_status (appindicator.IndicatorStatus.ACTIVE)
ind.set_attention_icon ("indicator-messages-new")
menu = Gtk.Menu()
for i in range(3):
buf = "Test-undermenu - %d" % i
menu_items = Gtk.MenuItem(buf)
menu.append(menu_items)
menu_items.show()
ind.set_menu(menu)
Gtk.main()
Example drawn from here.

Related

Python interpreter, Pygtk and GtkGrid

i've to use GtkGrid because i need to draw a rectangle for every item i have.
(i would add a Drawing area to every GtkGrid's cell and draw the rectangle by cairographics library)
But there was a problem: python 2.7 doesn't support GtkGrid, so i surfed the web and i simply changed the first line of my file (and installed python3.3).
#!/usr/bin/python3.3
try:
import pygtk
pygtk.require("2.0")
except:
print("PyGtk Not Availible")
sys.exit(1)
try:
import gtk
import gtk.glade
except:
print("GTK Not Availible")
sys.exit(1)
Now it cannot rescue anymore Pygtk or GTK libraries.. and with python2.7 all work fine..
Maybe the best solution would be to avoid newer python interpreters and change GtkGrid into something else..
Help me please
EDIT::
Just cut and pasted an example from Pygtk examples
#!/usr/bin/python3.3
# example drawingarea.py
import pygtk
pygtk.require('2.0')
import gtk
import operator
import time
import string
class DrawingAreaExample:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Drawing Area Example")
window.connect("destroy", lambda w: gtk.main_quit())
self.area = gtk.DrawingArea()
self.area.set_size_request(400, 300)
self.pangolayout = self.area.create_pango_layout("")
self.sw = gtk.ScrolledWindow()
...
With Python 2.7 it works, with Python 3.3: No module named 'pygtk'
Just to make this clear: You have to decide wether you want to use the gi.introspection bindings (which are up to date and mostly autogenerated) or the pygtk wrapper around gtk+-2.0 (hand crafted, as of now pygtk3 is still work in progress, correct me if I am wrong).
Mixing these will get you in trouble further or later.
Also your initial issue was, GtkGrid (part of gtk+-3.0) was part of the gi.introspection bindings which (usually) require Python 3.x.y, whereas you used pygtk2 with Python 2.7.x. Changing to Python 3.3 just made the bindings availiable to you.
In gtk2 GtkGrid was called GtkTable, see https://developer.gnome.org/gtk2/stable/GtkTable.html.
Try this:
#!/usr/bin/env python
# -*- coding: utf-8; -*-
from gi.repository import Gtk
grid = Gtk.Grid()
You need to use GTK+ 3, not python3.3 (in order to have a Gtk.Grid)... if i understand what you mean...
Edited.

How to make global keyboard shortcuts with python (and Gtk3)?

I want to make keyboard shortcuts like t, that would work, when the main window is closed (but process is running, as the programme has a unity appindicator). I saw a package keybinder, but it seems, one can't use it with Gtk3 and pygobject. Or can? Then how? If not, is there any other way to do that?
The application is for linux (ubuntu), I use python 2.7.
Keybinder works fine with python3, Gtk3, and pygi. There just wasn't a working example in the source tree.
#!/usr/bin/env python3
"""
example-gi-py3.py
Looked at a pull request that was built for py2.x, but
overwrote the original py instead of making a separate example.
I wouldn't have accepted that pull request either.
The keybinder.init() part wasn't in the original example.
aking1012.com#gmail.com
public domain
"""
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Keybinder', '3.0')
from gi.repository import Gtk
from gi.repository import Keybinder
def callback(keystr, user_data):
print ("Handling", user_data)
print ("Event time:", Keybinder.get_current_event_time())
Gtk.main_quit()
if __name__ == '__main__':
keystr = "<Ctrl><Alt>M"
Keybinder.init()
Keybinder.bind(keystr, callback, "keystring %s (user data)" % keystr)
print ("Press", keystr, "to handle keybinding and quit")
Gtk.main()
Notes:
Not thoroughly tested, but as a simple example it seems to work.
I use also Keybinder to activate a search entry field in a Gtk3 app:
from gi.repository import Keybinder
…
class MyApp:
…
Keybinder.init()
Keybinder.bind("<Ctrl>F", self.set_search_entry_focus)
…
def set_search_entry_focus(self, keystring):
self.search_entry.grab_focus()
http://lazka.github.io/pgi-docs/Keybinder-3.0/
But be aware, this will also steal focus if if you are using another app and your app is running in the background.

display a menu bar icon on osx

I'd like to have my application display an icon in OSX menu bar (top of screen where Growl sits). How would I do this using Python? (I understand this is not possible using wxPython but I am not after a wxPython specific solution).
Thanks!
An implementation of this may be found at:
https://web.archive.org/web/20080709014939/http://the.taoofmac.com/space/blog/2007/04/22/1745
http://the.taoofmac.com/space/blog/2007/04/22/1745
The API for displaying icons in the OS X menubar is called NSStatusItem. It's going to be difficult or impossible to use from a wxPython application, though -- you will probably have to write your application using PyObjC to use it effectively.
The rumps package makes this very easy. Here's an example from rumps's README:
import rumps
class AwesomeStatusBarApp(rumps.App):
#rumps.clicked("Preferences")
def prefs(self, _):
rumps.alert("jk! no preferences available!")
#rumps.clicked("Silly button")
def onoff(self, sender):
sender.state = not sender.state
#rumps.clicked("Say hi")
def sayhi(self, _):
rumps.notification("Awesome title", "amazing subtitle", "hi!!1")
if __name__ == "__main__":
AwesomeStatusBarApp("Awesome App").run()

How to embed some application window in my application using any Python GUI framework

I want some application to look like widget inside my Python application.
That's all. I dont need any interaction between them. I'm interested in solutions in any GUI toolkit for both windows and x windows.
It would be nice to have a solution with Tkinter but it's not crucial.
Using GTK on X windows (i.e. Linux, FreeBSD, Solaris), you can use the XEMBED protocol to embed widgets using gtk.Socket. Unfortunately, the application that you're launching has to explicitly support it so that you can tell it to embed itself. Some applications don't support this. Notably, I can't find a way to do it with Firefox.
Nonetheless, here's a sample program that will run either an X terminal or an Emacs session inside a GTK window:
import os
import gtk
from gtk import Socket, Button, Window, VBox, HBox
w = Window()
e = Button("Emacs")
x = Button("XTerm")
s = Socket()
v = VBox()
h = HBox()
w.add(v)
v.add(s)
h.add(e)
h.add(x)
v.pack_start(h, expand=False)
def runemacs(btn):
x.set_sensitive(False); e.set_sensitive(False)
os.spawnlp(os.P_NOWAIT, "emacs",
"emacs", "--parent-id", str(s.get_id()))
def runxterm(btn):
x.set_sensitive(False); e.set_sensitive(False)
os.spawnlp(os.P_NOWAIT, "xterm",
"xterm", "-into", str(s.get_id()))
e.connect('clicked', runemacs)
x.connect('clicked', runxterm)
w.show_all()
gtk.main()
Not enough reputation to comment on Glyphs answer. To make xterm work, in addition to the comments above one needs to also add
XTerm*allowSendEvents: True
to ~/.Xresources. (and perhaps reload those, with xrdb -load ~/.Xresources)

Undo with GTK TextView

I'm trying to keep dependencies to a minimum for a program I contribute to, it's a small text editor.
GTK Textview doesn't seem to come with a built-in undo function. Is there any reference implementation I've been missing on so far? Is everyone writing their own undo function for their TextView widgets?
I'll be happy about any sample code - most happy about python sample code, as our project is in python.
as a follwow-up: I ported gtksourceview's undo mechanism to python: http://bitbucket.org/tiax/gtk-textbuffer-with-undo/
serves as a drop-in replacement for gtksourceview's undo
(OP here, but launchpad open-id doesn't work anymore)
As far as I know, GTK TextView doesn't include an undo function. So while I am not familiar with Python's GTK library, I would think it doesn't have one.
The Ruby-GNOME2 project has a sample text editor that has undo/redo functionality. Basically they are connecting to the insert_text and delete_range signals of the TextView widget and recording the events and associated data in a list.
Depending on just how dependency-averse you are, and what kind of text editor you're building, GtkSourceView adds undo/redo among many other things. Very worth looking at if you want some of the other features it offers.
Use GtkSource
https://wiki.gnome.org/Projects/GtkSourceView
https://lazka.github.io/pgi-docs/GtkSource-3.0/
https://lazka.github.io/pgi-docs/GtkSource-3.0/classes.html
.
[Cmnd] + [Z] for undo (default)
[Cmnd] + [Shift] + [Z] for redo (default)
[Cmnd] + [Y] for redo (added manually)
example:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
gi.require_version('GtkSource', '3.0')
from gi.repository import GtkSource
import os
class TreeviewWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TreeviewWindow")
self.set_size_request(300, 300)
self.connect("key-press-event", self._key_press_event)
self.mainbox = Gtk.VBox(spacing=10)
self.add(self.mainbox)
self.textbuffer = GtkSource.Buffer()
textview = GtkSource.View(buffer=self.textbuffer)
textview.set_editable(True)
textview.set_cursor_visible(True)
textview.set_show_line_numbers(True)
self.mainbox.pack_start(textview, True, True, 0)
self.show_all()
def _key_press_event(self, widget, event):
keyval_name = Gdk.keyval_name(event.keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if ctrl and keyval_name == 'y':
if self.textbuffer.can_redo():
self.textbuffer.do_redo(self.textbuffer)
def main(self):
Gtk.main()
if __name__ == "__main__":
base = TreeviewWindow()
base.main()

Categories

Resources