Python interpreter, Pygtk and GtkGrid - python

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.

Related

Simple Pantheon panel applet on eOS?

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.

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.

Embed vte. How to do the same as this PyGtk app with wxPython?

It's my first post. I read a lot of subjects per day on Stackoverflow, and i appreciate to find there some help.
And today it's my turn to ask a question because i didn't find solution to my need.
I want to have a terminal in the app i'm writing; i(ve read a lot about a lots (subprocess, thread, pty, etc, etc), but i didn't find a simple solution as this one written with pygtk and vte. Here is the code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygtk
pygtk.require('2.0')
import gtk, vte
window = gtk.Window()
window.resize(600,400)
window.show()
term = vte.Terminal()
pid = term.fork_command('bash')
term.set_emulation('xterm')
term.show()
window.add(term)
window.show_all()
window.connect("destroy", lambda w: gtk.main_quit())
gtk.main()
Do you know a way to do that with wxPython?
Thanks a lot for your help, many thanks!! :)
Edit0:
I have to precise that:
i'm talking about a bash shell (as in the code above), not a python shell
i write it for linux
Edit1:
Thanks for your answers! :)
Joran, i've read already this question, and i've tried all the code proposed. Although it's interesting, that's not the way i want to do it as you can see in my question. It seems to be very difficult to write an app that recreate a terminal emulator.
That's why i'm searching for a solution like the code i proposed.
pythonm, i don't see the relation between your idea and my question...
Thanks for your help!
Edit2
Please look at this short code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import pty
shell = os.environ['SHELL']
script = open('typescript', 'w')
def read(fd):
data = os.read(fd, 1024)
script.write(data)
return data
pty.spawn(shell, read)
Any idea to "put" this in a widget with wxpython?
Edit3
Look that too:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pexpect
c = pexpect.spawn ('bash -i')
c.interact()
c.kill(1)
So simple and so easy to do...
Tell me if it's impossible to embed that in a wx widget.
I've seen this question come up a couple times in the last month or two. The answer is no, wxPython doesn't have this capability. You would have to use subprocess to launch something and communicate with it.

Get GTK+ theme colors with python and GObject introspection

With introduction of GObject introspection the way to access theme colors through widget.get_style() method is gone. I am interested on how to get theme colors when GTK+ is used through GOBject introspection. The solution should preferably work with both versions (2 and 3) but a solution for each of these is acceptable as well.
I'm not sure how to get it from gtk+-2.0, unless your using a pure gtk+-2.0 environment, in which case I think the old GtkStyle methods work. for example, assuming your not running a Gtk-3.0 environment like gnome-shell
import gi
# make sure you use gtk+-2.0
gi.require_version('Gtk', '2.0')
from gi.repository import Gtk
window = Gtk.Window()
...
style = window.get_style()
print style.lookup_color('fg_color')
I think that should still work under a gtk+-2.0 environment. I don't know for sure as my system is running gnome-shell, and can't easily try this out.
However this method has been deprecated and replaced by GtkStyleContext. If I use the above code in a gtk+-3.0 environment like gnome-shell it will run, but does not give me the information I'm after. What I get is
(False, <Gdk.Color(red=0, green=0, blue=0)>)
EDIT: Looking back at this, I think the above is still giving the correct info. The colour for fg_color is not found, as indicated by the first entry in the tuple result, which is False. Also the window must be visible for the colours to be found.
If I want colour information I want to use the new GtkStyleContext, for example
import gi
# make sure you use gtk+-3.0
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
window = Gtk.Window()
...
style_context = window.get_style_context()
print style_context.lookup_color('fg_color')
this will give me some real data, which is telling me the 'fg_color' has been found, due to the first entry in the tuple is True.
(True, <Gdk.Color(red=0.000000, green=0.000000, blue=0.000000, alpha=1.000000)>)
I hope this answers your question.

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