Active Topics

 


Reply
Thread Tools
omeriko9's Avatar
Posts: 385 | Thanked: 344 times | Joined on Jan 2010 @ Israel
#11
Originally Posted by McLightning View Post
class gui(threading.Thread):
__def __init__(self):
____self.a=0
____.......
____self.entrybox=self.wTree.get_widget('entry2')
____threading.Thread.__init__(self)
__def run(self):
____while 1:
______print self.a
______self.entrybox.set_text(a)
______self.a=self.a+1

gui().start()
gtk.main()

it is not working this way either
Can you please post full code, or add as an attachment if it's too long?
 
epage's Avatar
Posts: 1,684 | Thanked: 1,562 times | Joined on Jun 2008 @ Austin, TX
#12
Don't forget to initialize threads with gobject or else (I suspect) it holds on to the GIL while mainloop is processing and doesn't allowing other threads to run.

I've been using
Code:
	gtk.gdk.threads_init()
Also if you ever access GTK from outside of your UI thread, you need to be sure to grab the locks. I use the following helper

Code:
@contextlib.contextmanager
def gtk_lock():
	gtk.gdk.threads_enter()
	try:
		yield
	finally:
		gtk.gdk.threads_leave()
Code:
with gtk_lock():
	# do stuff
If you want an example of a threaded PyGTK program, checkout Dialcentral's source
https://garage.maemo.org/scm/?group_id=657

You can also queue tasks to run in the UI thread from your worker thread using idle_add. I use the following helper

Code:
def async(func):
	"""
	Make a function mainloop friendly. the function will be called at the
	next mainloop idle state.
	"""

	@functools.wraps(func)
	def new_function(*args, **kwargs):

		def async_function():
			func(*args, **kwargs)
			return False

		gobject.idle_add(async_function)

	return new_function
For a more advanced PyGTK threading example than Dialcentral I recommend The One Ring. Though it isn't GTK but DBus based, the principles are the same and the helpers I use in go_utils will work in both.
https://garage.maemo.org/scm/?group_id=1078
__________________
770, n810, n900, Ideapad S10-3t
TheOneRing, DialCentral, Gonvert, Quicknote, Multilist, ejpi, nQa, Waters of Shiloah
Programming Blog
 

The Following 3 Users Say Thank You to epage For This Useful Post:
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#13
Code:
import pygtk
import gtk
import hildon
import gtk.glade
from bluetooth import *
import time,threading
class gui(threading.Thread):
  def gon(self,data=None):
    data=self.got.get_text()
    print data
    if len(data)!=0:
      self.sock.send(data)

  def __init__(self):
    self.a=0
    self.program = hildon.Program()
    self.program.__init__()
    self.window = hildon.Window()
    self.program.add_window(self.window)
    self.window.connect("destroy", gtk.main_quit)
    self.glade_file = "sck.glade"
    self.wTree = gtk.glade.XML(self.glade_file)
    self.got = self.wTree.get_widget("entry1") 
    self.al = self.wTree.get_widget("entry2")
    btn = self.wTree.get_widget("button1")
    signals={'clicked':self.send}
    self.wTree.signal_autoconnect(signals)
    self.fx=self.wTree.get_widget("notebook1")        
    self.reparent_loc(self.fx, self.window)
    self.gtkWindow = self.wTree.get_widget("window1")
    self.gtkWindow.destroy()
    self.window.show()
    print 'connecting'
    self.sock=BluetoothSocket(RFCOMM)
    self.sock.connect(('00:1F:00:B5:3A:45',5))
    print "connected."

    threading.Thread.__init__(self)
  def run(self):
    while True:
      self.a=self.a+1
      self.got.set_text(self.a)
      print a
  def reparent_loc(self, widget, newParent):
    widget.reparent(newParent)

gui().start()
gtk.main()
this is the full code

Last edited by McLightning; 2010-04-03 at 14:34.
 
omeriko9's Avatar
Posts: 385 | Thanked: 344 times | Joined on Jan 2010 @ Israel
#14
Also, if you use threads with gtk, you need to call
gtk.gdk.threads_init().

pygtk (binding for python and gtk) holds the GIL (which is kind of a global lock for the process) unless threads_init() is called.

Try adding this line in the __init__.
 

The Following User Says Thank You to omeriko9 For This Useful Post:
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#15
i posted the full code please could you tell me what should i do about it?
edit:
yeaa great it worked
it worked

THANK YOU ALL :d:d

Last edited by McLightning; 2011-08-05 at 17:08.
 
omeriko9's Avatar
Posts: 385 | Thanked: 344 times | Joined on Jan 2010 @ Israel
#16
Originally Posted by McLightning View Post
i posted the full code please could you tell me what should i do about it?
I suggested what you should do if you want to use threads for background work, aka write to a file or print to the terminal or doing some calculations, but keep all the GUI updates to the main thread (called 'UI thread').

epage mentioned that as well but also extended this to how to handle the GUI itself from within child-threads (also called 'worker threads'), and suggested an alternative - give the gtk to handle your worker threads while it's available using the add_idle example he attached.

An advice - If it's your first work with treads, you might want to know more about threads in general and threads in python before diving into gtk's way for handling threads.
 

The Following User Says Thank You to omeriko9 For This Useful Post:
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#17
it is great im very excited it worked after adding gtk.gdk.threads_init()
thank you all for all helps
im really appreciated
 

The Following User Says Thank You to McLightning For This Useful Post:
Posts: 56 | Thanked: 31 times | Joined on Jul 2008 @ Austria
#18
Just use gobject.io_add_watch to be notified when there is data to read and spare yourself the ordeal of using threads.

http://faq.pygtk.org/index.py?req=sh...=faq20.016.htp

def handle_data(source, condition):
__data = source.recv(1024)
__if len(data) > 0:
____self.entrybox.set_text(self.entrybox.get_text( ) + data)
____# or maybe just self.entrybox.props.text += data
____#print data
____return True # call me again if something happens
__else: # end of file
____return False # stop calling me

sock=BluetoothSocket(RFCOMM)
#sock.settimeout(15) # ?
sock.connect(('00:1F:00:B5:3A:45',5))
gobject.io_add_watch(sock, gobject.IO_IN, handle_data)

What happens is that your GUI program ALSO watches your socket and should something arrive, it will call "handle_data" which then reads the chunk that arrived (that is fast).
After that, your GUI program resumes. Note that threads didn't enter the picture anywhere (not in the GTK library either).

Last edited by dannym; 2010-04-08 at 09:10.
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#19
i made a couple of apps that im using myself
one is for texting over n95 8gb and received text messages via n95 8gb
 
Reply


 
Forum Jump


All times are GMT. The time now is 20:00.