GTK Graphics Sample Activity: Difference between revisions
Jump to navigation
Jump to search
(Initial revision) |
No edit summary |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{Translations}} |
|||
Here is an activity which demonstrates using GTK (specifically PyGTK) to do graphics in a Sugar Activity. The activity draws a green square inside of a black square using GTK drawing commands. When you press a key it quits. |
Here is an activity which demonstrates using GTK (specifically PyGTK) to do graphics in a Sugar Activity. The activity draws a green square inside of a black square using GTK drawing commands. When you press a key it quits. |
||
Line 7: | Line 8: | ||
# in the Sugar environment. |
# in the Sugar environment. |
||
# |
# |
||
⚫ | |||
import pygtk |
import pygtk |
||
pygtk.require('2.0') |
pygtk.require('2.0') |
||
⚫ | |||
import gtk |
import gtk |
||
import sys |
import sys |
||
Line 24: | Line 25: | ||
""" |
""" |
||
def __init__(self, handle): |
def __init__(self, handle): |
||
activity.Activity.__init__(self, handle) |
activity.Activity.__init__(self, handle) |
||
# remove any children of the window that Sugar may have added |
# remove any children of the window that Sugar may have added |
||
for widget in self.get_children(): |
for widget in self.get_children(): |
||
self.remove(widget) |
self.remove(widget) |
||
self.set_title("Drawing Area Example") |
self.set_title("Drawing Area Example") |
||
Line 43: | Line 44: | ||
self.area = self.window |
self.area = self.window |
||
self.gc = self.area.new_gc() |
self.gc = self.area.new_gc() |
||
# Set up the colors to draw with |
# Set up the colors to draw with |
||
self.colormap = self.gc.get_colormap() |
self.colormap = self.gc.get_colormap() |
||
Line 49: | Line 50: | ||
self.colors['green'] = self.colormap.alloc_color('green') |
self.colors['green'] = self.colormap.alloc_color('green') |
||
self.colors['black'] = self.colormap.alloc_color('black') |
self.colors['black'] = self.colormap.alloc_color('black') |
||
def keypress_cb(self, widget, event): |
def keypress_cb(self, widget, event): |
||
"""Handle a key press""" |
"""Handle a key press""" |
||
Line 62: | Line 63: | ||
return True |
return True |
||
[[Category:Tutorials]] |
|||
[[Category:Activities]] |
Latest revision as of 04:59, 13 July 2007
Here is an activity which demonstrates using GTK (specifically PyGTK) to do graphics in a Sugar Activity. The activity draws a green square inside of a black square using GTK drawing commands. When you press a key it quits.
# # Minimal GTK Graphics Activity # # This demo shows how to set things up to do GTK graphics # in the Sugar environment. # import pygtk pygtk.require('2.0') import gtk import sys from sugar.activity import activity class MinimalGtkGraphicsActivity(activity.Activity): """Minimal GTK Graphics activity. Sets up the window for drawing and draws a rectangle inside another rectangle when the window is shown to the user. Waits for a key press and then quits. """ def __init__(self, handle): activity.Activity.__init__(self, handle) # remove any children of the window that Sugar may have added for widget in self.get_children(): self.remove(widget) self.set_title("Drawing Area Example") # Set up event handlers self.connect("destroy", lambda w: gtk.main_quit()) self.connect("expose-event", self.area_expose_cb) self.connect("key_press_event", self.keypress_cb) self.show() # Set up the drawing area and graphics context self.area = self.window self.gc = self.area.new_gc() # Set up the colors to draw with self.colormap = self.gc.get_colormap() self.colors = {} self.colors['green'] = self.colormap.alloc_color('green') self.colors['black'] = self.colormap.alloc_color('black') def keypress_cb(self, widget, event): """Handle a key press""" sys.exit() def area_expose_cb(self, area, event): """Window exposed callback, includes drawing the boxes""" self.gc.set_foreground(self.colors['black']) self.area.draw_rectangle(self.gc, True, 100, 100, 300, 300) self.gc.foreground = self.colors['green'] self.area.draw_rectangle(self.gc, True, 200, 200, 100, 100) return True