Last active
December 11, 2015 15:09
-
-
Save neilus/4619454 to your computer and use it in GitHub Desktop.
Exploring threads with pyGTK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import gtk, gobject | |
class ProgressBar: | |
def __init__(self): | |
window = gtk.Window() | |
window.set_default_size(200, -1) | |
vbox = gtk.VBox(False, 5) | |
self.progressbar = gtk.ProgressBar() | |
self.check_text = gtk.CheckButton("Display text") | |
vbox.pack_start(self.progressbar) | |
vbox.pack_start(self.check_text) | |
window.connect("destroy", lambda w: gtk.main_quit()) | |
self.check_text.connect("toggled", self.text) | |
window.add(vbox) | |
window.show_all() | |
self.run() | |
def run(self): | |
gobject.timeout_add(500, self.update) | |
def update(self): | |
if self.progressbar.get_fraction() >= 1.0: | |
value = 0.0 | |
else: | |
value = self.progressbar.get_fraction() + 0.1 | |
self.progressbar.set_fraction(value) | |
if self.check_text.get_active(): | |
percent = value * 100 | |
percent = str(int(percent)) | |
self.progressbar.set_text(percent + "%") | |
return True | |
def text(self, widget): | |
if self.check_text.get_active() == False: | |
self.progressbar.set_text("") | |
ProgressBar() | |
gtk.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import threading | |
import random, time | |
import gtk | |
#Initializing the gtk's thread engine | |
gtk.threads_init() | |
class FractionSetter(threading.Thread): | |
"""This class sets the fraction of the progressbar""" | |
#Thread event, stops the thread if it is set. | |
stopthread = threading.Event() | |
def run(self): | |
"""Run method, this is the code that runs while thread is alive.""" | |
#Importing the progressbar widget from the global scope | |
global progressbar | |
print "fut a fut a fut a" | |
#While the stopthread event isn't setted, the thread keeps going on | |
while not self.stopthread.isSet() : | |
# Acquiring the gtk global mutex | |
gtk.threads_enter() | |
gtk.main_iteration() | |
# Releasing the gtk global mutex | |
gtk.threads_leave() | |
#Delaying 100ms until the next iteration | |
time.sleep(0.1) | |
def stop(self): | |
"""Stop method, sets the event to terminate the thread's main loop""" | |
self.stopthread.set() | |
def main_quit(obj): | |
"""main_quit function, it stops the thread and the gtk's main loop""" | |
#Importing the fs object from the global scope | |
global fs | |
#Stopping the thread and the gtk's main loop | |
fs.stop() | |
gtk.main_quit() | |
#Gui bootstrap: window and progressbar | |
window = gtk.Window() | |
progressbar = gtk.Spinner() | |
progressbar.start() | |
window.add(progressbar) | |
window.show_all() | |
#Connecting the 'destroy' event to the main_quit function | |
window.connect('destroy', main_quit) | |
#Creating and starting the thread | |
fs = FractionSetter() | |
fs.start() | |
# time.sleep(10) | |
gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment