Active Topics

 


Reply
Thread Tools
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#1
hi everybody
im using python
i have a problem about "while loops"
but the problem is when i run gtk.main() function it is not allowing me to run another loop. :/ and if i run another loop it is stopping gtk window
:/
def read():
global sock
while 1:
a=sock.recv(1024)
if len(a)==0: break
print a
this is the function i need to run meanwhile gtk is running :/
please help
i googled a lil bit but i couldnt find a proper tutorial on handling multitasking
 
Posts: 1,101 | Thanked: 1,185 times | Joined on Aug 2008 @ Spain
#2
Look for threads and threading modules in python docs.
Beware that you can only call gtk from the main thread.
 

The Following User Says Thank You to maacruz For This Useful Post:
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#3
class gui(threading.Thread):
__def __init__(self):
____......
____.....
____self.window.show_all()
____threading.Thread.__İnit__(self)
__def run(self):
____gtk.main()

while 1:
__print 'a'
__gui().start()

it supposed to print 'a' forever
but after the gtk window showed up it stopped
please help :/
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#4
most of the tutorials on web are about "for loops" and handling them with threading module
but i guess gtk.main() function has a while loop in it
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#5
 
omeriko9's Avatar
Posts: 385 | Thanked: 344 times | Joined on Jan 2010 @ Israel
#6
Originally Posted by McLightning View Post
http://stackoverflow.com/questions/8...hile-true-loop
i found this but :/ i didnt get it
Queue handle multiple threads needs to run at the same time, without the need to take care of locking. If you are not familiar with the term 'lock' in the threads context, you might want to read about that - it's a fundamental part of threading.

Anyway, as you've being said earlier by maacruz, you need to verify that you call gtk.main() from the main thread only, and in the code example you posted, you are not doing that (run() is called by the child threads, therefore gtk.main() is being called by the child threads).

EDIT: to get the expected behavior from the code, replace "print" with the "gtk.main()", and take "gtk.main()" outside the any class or loop (place at the bottom after gui.start()).

EDIT2: There's really no difference between "for", "loop" and "while", all of them interpret to the same assembly calls, just with different handling for the conditions.

Last edited by omeriko9; 2010-04-03 at 13:04.
 

The Following 2 Users Say Thank You to omeriko9 For This Useful Post:
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#7
import threading
class gui(threading.Thread):
__def __init__(self):
____self.a=0
____.......
____threading.Thread.__init__(self)
__def run(self):
____while 1:
______print self.a
______self.a=self.a+1

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

but in this case print function stopping after gtk.main() shows up
im sorry i know im asking a lot :/ :/ im a total noob :/
thanks for help

EDIT: it starts to print again after i close gtkwindow

Last edited by McLightning; 2010-04-03 at 13:51.
 
omeriko9's Avatar
Posts: 385 | Thanked: 344 times | Joined on Jan 2010 @ Israel
#8
Originally Posted by McLightning View Post
import threading
class gui(threading.Thread):
__def __init__(self):
____self.a=0
____.......
____threading.Thread.__init__(self)
__def run(self):
____while 1:
______print self.a
______self.a=self.a+1

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

but in this case print function stopping after gtk.main() shows up
im sorry i know im asking a lot :/ :/ im a total noob :/
thanks for help

EDIT: it starts to print again after i close gtkwindow
It doesn't stop. I think you just can't see it because you see the GUI now.

try adding writing a to a file, you can see it keeps after you see the gui:

Code:
class gui(threading.Thread):
__def __init__(self):
____self.a=0
____.......
____threading.Thread.__init__(self)
__def run(self):
____f = open('/home/user/t.txt', 'a')
____while 1:
______print self.a
______f.write(str(self.a))
______self.a=self.a+1

gui().start()
gtk.main()
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#9
no i also see terminal window
i mean terminal window and gtk windows gets opened same time
and i can see terminal windows while gtkwindow is still running in the background
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#10
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

EDIT:
from bluetooth import *

class gui(threading.Thread):
__def __init__(self):
____.......
____self.entrybox=self.wTree.get_widget('entry2')
____self.sock=BluetoothSocket(RFCOMM)
____self.sock.connect((host,port))
____print 'connected'

____threading.Thread.__init__(self)
__def run(self):
____while 1:
______data=self.sock.recv(1024)
______self.entrybox.set_text(data)


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

what i want is to set this device to send and get data whenever it wants.
i got a button in gui that starts send function and it work OK
but problem is whole about this reading function :/

Last edited by McLightning; 2010-04-03 at 14:21.
 
Reply


 
Forum Jump


All times are GMT. The time now is 17:28.