0

i'm working on a Gtk.DrawingArea because i want simply draw a rectangle. I've cut and pasted the example of DrawingArea from Gtk examples, cutting off useless code for my purpose.

Here below the code (don't care about indentation problems, it's all ok, Geany IDE sucks):

#!/usr/bin/env python
# -*- coding: utf-8; -*-
from gi.repository import Gtk
import pygtk
pygtk.require('2.0')



class collega_GUI:



    def __init__(self):

            self.__builder = Gtk.Builder()
            self.__builder.add_from_file('prova.glade')

            self.__area = self.__builder.get_object('drawingarea1')

            self.__style = self.__area.get_style()
            self.__gc = self.__style.fg_gc[gtk.STATE_NORMAL]
            self.__pangolayout = self.__area.create_pango_layout("")
            self.__area.draw_rectangle(self.__gc, True, 0, 0, 20, 20)

            self.__pangolayout.set_text("1")
            self.__area.draw_layout(self.gc, 0, 50, self.__pangolayout)

            self.__window = self.__builder.get_object('window1')
            self.__window.show()


if __name__=='__main__':
    prova = collega_GUI()
    Gtk.main()

So the python interpreter says me:

AttributeError: 'Style' object has no attribute 'fg_gc'

Please help me, i've read the documentation at (http://www.pygtk.org/pygtk2tutorial/sec-DrawingAreaWidgetAndDrawing.html) but i can't find the error

FrancescoN
  • 2,146
  • 12
  • 34
  • 45
  • 1
    i'm really interested why some1 has downvoted the question.. – FrancescoN Jan 16 '14 at 15:01
  • 1
    Would it be possible for you to fix the indentation. I think the next thing for you to do is to find out what type `self.__style` is, and what attributes it has. – David Heffernan Jan 16 '14 at 17:21
  • I can't edit the code, because it would reply me it isn't correctly formatted. **type(self.__style) -> gi.repository.Gtk.Style** – FrancescoN Jan 16 '14 at 20:45
  • Try harder. Please fix indentation. Did you do the debugging I suggested? – David Heffernan Jan 16 '14 at 20:47
  • It seems it has just the **fg** attribute – FrancescoN Jan 17 '14 at 00:51
  • Now it raises another error: **AttributeError: 'gi.repository.Gtk' object has no attribute 'STATE_NORMAL'**. But man, look at the example of Gtk about GtkDrawingArea linked above.. it works.. why doesn't it happen in mine? – FrancescoN Jan 17 '14 at 00:55

1 Answers1

1

I think your imports should look like this:

import pygtk
pygtk.require('2.0')
import gtk

You are mixing the pygtk wrapper and the gi.introspection bindings which are two different things! And potentially even mixing gtk2 with gtk3 widgets!


Also keep in mind: that tutorial's last update was in 2005!

drahnr
  • 6,782
  • 5
  • 48
  • 75
  • Ok thank you very much for the support on this topic and the one about GtkGrid, because they were strictly related. – FrancescoN Jan 17 '14 at 11:02