PDA

View Full Version : Python: Why doesn't this work?


BrentDC
05-19-2009, 05:43 PM
It seems like I've spent hours today trying to get something seemingly very simple to work, but have been unable to *bangs head on table*.

I'm trying to create a class, that when called, spawns into a new thread (as to allow the calling code to keep running) and prints out the current contents of the clipboard every 2 seconds. In reality, I'll create gtk.MenuItems from the polled history, so this is more or less just a test to see if I can get the basics of the code to work.

Here it is:

import threading
import gtk
import time

class Clipboard(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
while(True): # Forever loop
self.clip = self.get_clipboard()
time.sleep(2)

def get_clipboard(self):
clip = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
print 1
text = clip.request_text(self.get_text)

def get_text(self, clipboard, text):
print 2
print text

Clipboard().start()

This code doesn't work at all. self.get_text is never called (that is why the print 1 and print 2 statements are there). I'm wondering, why?

Is it because I'm not actually using the gtk mainloop? Or is the threading messing with something?

This is mostly a backend class, and I didn't want to involve gtk in the first place, but there doesn't seem to be a way to access the clipboard otherwise.

Any help, and suggestions on a more elegant solution, is very much needed!

Thanks.

attila77
05-19-2009, 05:46 PM
import threading
import gtk
import time

class Clipboard(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
while(True): # Forever loop
self.clip = self.get_clipboard()
time.sleep(2)

def get_clipboard(self):
clip = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
print 1
text = clip.request_text(self.get_text)

def get_text(self, clipboard, text):
print 2
print text

Clipboard().start()

BrentDC
05-19-2009, 07:21 PM
Hi Attila,

As far as I can tell, that still doesn't work. 2 still isn't printed (nor is the clipboard contents).

daperl
05-19-2009, 08:34 PM
Try calling this first:

gtk.gdk.threads_init()

BrentDC
05-19-2009, 09:13 PM
Where would that go? In __init__ or outside the class or inside a specific function? Sorry, I've never used threading before :o (which is probably painfully obvious).

daperl
05-19-2009, 09:48 PM
I put it here:

if __name__ == "__main__":
gtk.gdk.threads_init()

...

gtk.main()

Do you have a main section like the one above? If not, I would put it above this line:

Clipboard().start()

EDIT:

I think it's only suppose to be called once per main loop.

danielwilms
05-20-2009, 02:46 AM
hey,

daperl is right, you have to initialise the thread in the main loop otherwise it would not work. BTW, where is your main loop?!? ;) To get a better understanding try this (http://blogs.operationaldynamics.com/andrew/software/gnome-desktop/gtk-thread-awareness.html) and some help tutorial how to implement it you will find here (http://unpythonic.blogspot.com/2007/08/using-threads-in-pygtk.html), even though I do not agree with the title of the blog entry ;) It might be easier to blog than to put it into a wiki, but it is definitely easier to find in a properly organised wiki. Actually there were so many questions related to Qt and GTK in the last couple of days which could be interesting for everyone, that I think it would be worth it to create a Qt and GTK Q&A section in the wiki as the first starting point with small issues (mostly related to maemo) or like this here...what do you think?

Cheers Daniel

attila77
05-20-2009, 04:27 AM
Hi Attila,

As far as I can tell, that still doesn't work. 2 still isn't printed (nor is the clipboard contents).

That's related to gtk, I just added the run so the threading works. I thought this was just a snippet, as people say here, you need to start a main loop somewhere.

BrentDC
05-20-2009, 03:37 PM
Thanks for all the replies! I followed everyones advice -- no seriously, I used Attila's run() function, daperl's gtk.gdk.threads_init() code, and daniel's threading and mainloop tips -- and now it actually works.

The only problem now, is I'm getting the strangest xlib crashes occasionally, I think my threading is "not quite right". I'll look into it.

A round of Thanks in me! :)

Edit: I (think) I fixed the crashing by wrapping all gtk calls in my clipboard class with gtk.gdk.threads_enter() and *_leave(). :)

BrentDC
05-20-2009, 03:58 PM
@Daniel:

If you think that there are some frequently asked questions about GTK/QT programming, that specifically relates to Maemo, then I think putting that in the Wiki would be great. :)

jeremiah
05-20-2009, 04:30 PM
What I would like to know is what is Nokia's position on GTK versus Qt - which toolset does Nokia expect developers to prioritize?

qole
05-20-2009, 05:18 PM
GTK is the "legacy" toolkit, but Qt certainly seems like it is really important to Nokia these days.

Hmm, the Qt logo is a little higher and on the right side (as opposed to GTK's tri-colour box on the mid-left), so maybe it will become the new toolkit-of-choice?

http://maemo.org/static/e/eb42356042ac11ddbc5f8dc15ddf368c368c_maemo_overvie w.png (http://maemo.org/intro/)

attila77
05-20-2009, 06:05 PM
So, you're saying gstreamer is the one to rule them all ? :D

BrentDC
05-20-2009, 06:10 PM
So, you're saying gstreamer is the one to rule them all ? :D

Just maybe (http://talk.maemo.org/showthread.php?t=29072)... ;)

qgil
05-21-2009, 10:41 AM
What I would like to know is what is Nokia's position on GTK versus Qt - which toolset does Nokia expect developers to prioritize?

GTK+ if you are familiar with it and/or you prefer to use the official toolkit in Maemo 5.

Qt if you don't have a strong opinion and/or you have mid term plans involving Nokia devices, not only with Maemo but also with Symbian.

If you want to discuss more please open a new thread. It was really a coincidence that I hit this question here. :)