Intro
I am new to GTK+ programming and am having difficulty understanding why the following does not block.
if self.journal:
# Pump GTK messages.
while gtk.events_pending(): gtk.main_iteration()
According to the PyGtk reference on gtk.main_iteration(), the default value for block is True, which should, seemingly, prevent the local code from running until events have been processed.
Yet, I don’t see this happening. Perhaps, a clue is in the # Pump GTK messages. comment. But I don’t understand what the comment is trying to convey to me.
My Question
So, my question is What does it mean to “Pump GTK messages?” Please be as low-level as possible. I understand that GTK is my widget toolkit and is designed to handle events.
Background
I am following a Sugar tutorial at flossmanuals using the SimCom activity. This is the section of code (to provide context) from which the snippet above is pulled.
#!/usr/bin/python
# SimCom.py
"""
Copyright (C) 2011 Peter Hewitt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
"""
import g,pygame,utils,sys,buttons,slider,load_save
try:
import gtk
except:
pass
import sim
class SimCom:
def __init__(self):
self.journal=True # set to False if we come in via main()
self.canvas=None # set to the pygame canvas if we come in via activity.py
.
.
.
def run(self):
g.init()
if not self.journal: utils.load()
self.sim=sim.Sim()
load_save.retrieve()
self.buttons_setup()
if g.saved_n==0: buttons.off('cyan')
self.slider=slider.Slider(g.sx(23.4),g.sy(20.2),5,utils.GREEN)
if self.canvas<>None: self.canvas.grab_focus()
ctrl=False
pygame.key.set_repeat(600,120); key_ms=pygame.time.get_ticks()
going=True
while going:
if self.journal:
# Pump GTK messages.
while gtk.events_pending(): gtk.main_iteration()
# Pump PyGame messages.
for event in pygame.event.get():
if event.type==pygame.QUIT:
if not self.journal: utils.save()
going=False
elif event.type == pygame.MOUSEMOTION:
g.pos=event.pos
g.redraw=True
if self.canvas<>None: self.canvas.grab_focus()
elif event.type == pygame.MOUSEBUTTONDOWN:
g.redraw=True
if g.help_on: g.help_on=False
elif event.button==1:
if self.do_click():
pass
elif self.slider.mouse():
pass # level changed
else:
bu=buttons.check()
if bu!='': self.do_button(bu); self.flush_queue()
elif event.button==3:
self.right_click()
elif event.type == pygame.KEYDOWN:
# throttle keyboard repeat
if pygame.time.get_ticks()-key_ms>110:
key_ms=pygame.time.get_ticks()
if ctrl:
if event.key==pygame.K_q:
if not self.journal: utils.save()
going=False; break
else:
ctrl=False
if event.key in (pygame.K_LCTRL,pygame.K_RCTRL):
ctrl=True; break
self.do_key(event.key); g.redraw=True
self.flush_queue()
elif event.type == pygame.KEYUP:
ctrl=False
if not going: break
self.update()
if g.redraw:
self.display()
if g.version_display: utils.version_display()
g.screen.blit(g.pointer,g.pos)
pygame.display.flip()
g.redraw=False
g.clock.tick(40)
if __name__=="__main__":
pygame.init()
pygame.display.set_mode()
game=SimCom()
game.journal=False
game.run()
pygame.display.quit()
pygame.quit()
sys.exit(0)
Right, But the code:
first checks if there are any events, and only runs
gtk.main_iteration()if there are. So there’s always at least one event to process, which means it will never block.gtk.main_iteration() does one iteration of the gtk main loop, which will process and handle the pending events (mouse events, screen redraw requests, key input, audio input/output, whatever gtk needs to do to drive the UI and handle requests from the operating system/graphics interface(The X11 server in case of *nix)). Often such events results in dispatching callbacks that you, or a library like pygame, have registred.