[pygtk] Trying to change the "pen color" just before I draw a line

Rick Hightower richardhightower at gmail.com
Thu Sep 3 08:29:15 WST 2009


I need to be able to draw different color lines.

For starters, I am trying to draw a blue line as follows:

gdk_color = gtk.gdk.color_parse("Blue")
gc.get_colormap().alloc_color(gdk_color)
gc.set_foreground(gdk_color)
   self.area.window.draw_line(gc, 0, 0, 80, 70)

Yet despite my best efforts the line is always black:

When I have done similar things in C it looked like this (and worked):

    GdkColor color;
    /*Select the color(green) to plot the HR graph on screen*/
    gc = gdk_gc_new(GTK_WIDGET(HrDarea)->window);
    gdk_color_parse("Green", &color);
    gdk_colormap_alloc_color(gdk_colormap_get_system(), &color, FALSE,
TRUE);
    gdk_gc_set_foreground(gc, &color);
    gdk_draw_line(GTK_WIDGET(HrDarea)->window, gc,
                          xIndexPrev, yIndexPrev, xIndex, yIndex);



What is the equivalent to the above in Python?


Here is the full program. It changes the background color and redraws.
I need to be able to draw different color lines.

import pygtk
pygtk.require("2.0")
import gtk
from gtk import Window, Button

class Base:
def handle_color_change(self, widget, data) :
self.color = data
if self.color == "Red":
gdk_color = gtk.gdk.color_parse("Red")
elif self.color == "Green":
gdk_color = gtk.gdk.color_parse("Green")
else:
gdk_color = gtk.gdk.color_parse("Blue")
         style = self.area.get_style().copy()
         style.bg[gtk.STATE_NORMAL] = gdk_color
         self.area.set_style(style)
self.area.queue_draw()

def __init__(self):
self.color = None
self.window = Window(gtk.WINDOW_TOPLEVEL)
         self.window.connect("destroy", lambda w: gtk.main_quit())

         self.mainPane = gtk.VBox(False, 0)
self.window.add(self.mainPane)
# create the toolbar
         self.toolbar = gtk.HBox(False, 0)
self.green = Button('Green')
self.red = Button ('Red')
self.toolbar.pack_start(self.green, True, True, 0)
self.toolbar.pack_start(self.red, True, True, 0)
self.mainPane.pack_start(self.toolbar, True, True, 0)
self.red.connect("clicked", self.handle_color_change, "Red")
self.green.connect("clicked", self.handle_color_change, "Green")


#Create the drawing area
         self.area = gtk.DrawingArea()
         self.area.set_size_request(400, 300)
self.area.connect("expose-event", self.area_expose)
self.mainPane.pack_start(self.area, True, True, 0)

self.window.show_all()

def area_expose(self, area, event):
print ("area exposed")
     gc = self.area.get_style().fg_gc[gtk.STATE_NORMAL]
gdk_color = gtk.gdk.color_parse("Blue")
gc.get_colormap().alloc_color(gdk_color)
gc.set_foreground(gdk_color)
   self.area.window.draw_line(gc, 0, 0, 80, 70)
return False

def main(self):
gtk.main()

if __name__ == "__main__":
base = Base()
base.main()







Back in the day I used to do a lot of Windows API C graphics programming,
but it has been many years and I am a bit fuzzy on how things work in the
GTK.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20090902/404e198a/attachment-0001.htm 


More information about the pygtk mailing list