'gi.repository.Gtk' object has no attribute 'gdk'
Asked Answered
Q

2

7

I'm trying to create a multiple threading with GTK. Gtk.gdk is needed but i received error about no gdk attribute. I'm using a Raspberry Pi with Raspbian.

This is how i import GTK library.

try:  
    import pygtk
    pygtk.require("2.0")  
except:  
    pass  

try:  
    from gi.repository import Gtk
except:  
    print("GTK Not Available")
    sys.exit(1)

Gtk.gdk.threads_init()

and this is the error i received.

AttributeError'gi.repository.Gtk' object has no attribute 'gdk'

Any idea?

Updates : I'm following this tutorial http://faq.pygtk.org/index.py?req=show&file=faq14.023.htp which are using both GObject.threads_init() & Gtk.gdk.threads_init(). I have no problem using GObject but gdk.

Quickwitted answered 31/3, 2013 at 9:36 Comment(5)
what is the point of requiring pygtk and then ignoring the exception when "require" fails? you should decide whether you are using the old pygtk or gi.repositoryInterlocution
This is what i get from some tutorials. gi.repository is the one i'm using. I had tried both pygtk and gi.repository but both of them do not have gdk, which i don't understand why. I had go through the gtk documentation manual, which gdk should be there.Quickwitted
You are confusing the old pygtk API where one would import gtk and then access gtk.<identifier> and gtk.gdk.<identifier>. This is what the FAQ entry you wrote in the comment is using. For GTK3 the pygtk bindings are no longer maintained, and one is supposed to switch to gobject-introspection. That is the 2nd style (not mentioned in the FAQ, which is the old PyGTK FAQ), from gi.repository import Gtk, Gdk and then access Gtk.<identifier> and Gdk.<identifier>.Interlocution
Thanks for your explanation. I did tried with import gtk and gtk.<identifier> but it is like not threading. My UI will continue freeze until the task performs finish. I could hardly find any reference about gdk.<identifier> threading, all is using gtk.gdk.<identifier>. What if I hope to use gtk.gdk.<identifier>? by importing the old pygtk?Quickwitted
It is getting very hard to follow you at this point. Maybe you could post a separate question that describes the threading problem that you had? Whatever you do, you should not mix old and new syntaxes - pick between import gtk and from gi.repository import Gtk, and use it consistently.Interlocution
F
4

I think the equivalent of the old-style gtk.gdk.threads_init() is:

from gi.repository import Gdk
Gdk.threads_init()

However, as the FAQ warns, threading is not a clean way to achieve this goal. A much better way is to use GObject.idle_add to run a function whenever the GUI is idle.


"""Show a shell command's output in a gtk.TextView without freezing the UI"""

import os
import locale
import subprocess
import shlex
import gi.repository.Gtk as gtk
from gi.repository import GObject
PIPE = subprocess.PIPE

encoding = locale.getpreferredencoding()


def utf8conv(x):
    return unicode(x, encoding).encode('utf8')


class MyWindow:
    def __init__(self):
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.PolicyType.AUTOMATIC, gtk.PolicyType.AUTOMATIC)
        textview = gtk.TextView()
        textbuffer = textview.get_buffer()
        sw.add(textview)
        win = gtk.Window()
        win.resize(300, 500)
        win.connect('delete-event', gtk.main_quit)

        self.button_sim = gtk.Button(u"Press me!")
        self.button_abort = gtk.Button("Abort")
        self.button_quit = gtk.Button("Quit")

        command = 'ls -R %s' % (os.getcwd(),)
        self.button_sim.connect(
            "clicked", self.on_button_clicked, textview, textbuffer, command)
        self.button_abort.connect("clicked", self.on_abort)
        self.button_quit.connect("clicked", self.main_quit)

        vbox = gtk.VBox()
        vbox.pack_start(self.button_sim, expand=False, fill=False, padding=0)
        vbox.pack_start(self.button_abort, expand=False, fill=False, padding=0)
        vbox.pack_start(self.button_quit, expand=False, fill=False, padding=0)
        vbox.pack_start(sw, expand=True, fill=True, padding=0)
        win.add(vbox)
        win.show_all()

    def read_output(self, view, buffer, command):
        yield True  # allow the UI to refresh
        proc = subprocess.Popen(
            shlex.split(command), stderr=PIPE, stdout=PIPE)
        while True:
            if self.job_aborted:
                print('user aborted')
                proc.terminate()
                break

            try:
                line = proc.stdout.readline()
                if line:
                    it = buffer.get_end_iter()
                    buffer.place_cursor(it)
                    buffer.insert(it, utf8conv(line))
                    view.scroll_to_mark(buffer.get_insert(), 0.1,
                                        use_align=False, xalign=0.5, yalign=0.5)

            except IOError:
                pass

            yield True

        yield False

    def on_button_clicked(self, button, view, buffer, command):
        self.job_aborted = False
        GObject.idle_add(self.read_output(view, buffer, command).next)

    def on_abort(self, button):
        self.job_aborted = True

    def main_quit(self, obj):
        self.job_aborted = True
        gtk.main_quit()


if __name__ == "__main__":
    app = MyWindow()
    gtk.main()
Found answered 31/3, 2013 at 9:43 Comment(4)
Thanks for your reply, I'm using this as well without problem. I'm trying to follow this tutorial faq.pygtk.org/index.py?req=show&file=faq14.023.htp which used both GObject.threads_init() & Gtk.gdk.threads_init()Quickwitted
I had tried from gi.repository import Gdk Gdk.threads_init() aswell. It runs without error. however it's like not threading. My UI continues freezing until the task performs finish. What i'm thinking is Gdk equal to Gtk.gdk?Quickwitted
AttributeError: 'gi.repository.Gdk' object has no attribute 'Pixbuf'Schiffman
I actually have many AttributeErrors in different parts, like AttributeError: 'gi.repository.Gtk' object has no attribute 'settings_get_for_screen'. It seems that gi.repository.Gtk has many differences with the standard one gtk. How to solve this?Schiffman
S
0

Since you are using gtk from gi.repository.Gtk, you could try to get the gdk in a similar way but please note that its attribute might differ from the original gtk.gdk.

from gi.repository import Gdk as gdk
Skivvy answered 14/12, 2020 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.