PyGTK contesti grafici e colori assegnazione
Domanda
Ho cercato su questo, ma nulla è quello che sto cercando.
http://www.mail-archive.com/pygtk@daa.com.au/msg10529 .html - Nessuno gli rispose. Questo è esattamente quello che sto vivendo. Quando ho creato il primo piano su un contesto grafico, non sembra cambiare in realtà.
Sono stato attraverso il tutorial e FAQ, ma nessuno dire molto. Loro o semplicemente usare i contesti in bianco e nero o ti danno i collegamenti interrotti. Sto pensando sinistra forse è un bug. Ma il bambino in me dice Sto solo manca qualcosa e io continuo a ignorare il fatto che ho un'alternativa di lavoro. Questo sarebbe meglio però. E più mi trovo in questo, più mi sto andando avere bisogno di questi contesti e colori.
Ecco il mio frammento di codice.
def CreatePixmapFromLCDdata(lcdP, ch, widget):
width = lcdP.get_char_width()
height = lcdP.get_char_height()
# Create pixmap
pixmap = gtk.gdk.Pixmap(widget.window, width, height)
# Working graphics contexts, wrong color
black_gc = widget.get_style().black_gc
white_gc = widget.get_style().white_gc
char_gc = widget.window.new_gc()
colormap = char_gc.get_colormap()
bg_color = NewColor(text="#78a878", colormap=colormap)
print "Before", char_gc.foreground.red, char_gc.foreground.green, char_gc.foreground.blue
char_gc.set_foreground(bg_color)
print "AFter", char_gc.foreground.red, char_gc.foreground.green, char_gc.foreground.blue
fg_color = NewColor(text="#113311", colormap=colormap)
pixmap.draw_rectangle(char_gc, True, 0, 0, width, height)
char_gc.foreground = fg_color
for j in range(lcdP.dots['y']):
k = lcdP.pixels['y']*j
for i in range(lcdP.dots['x']):
if 1<<(lcdP.dots['x']-1-i) & ch[j] == 0: continue
m = i*lcdP.pixels['y']
for jj in range(k, k+lcdP.pixels['y']-1):
for ii in range(m+1, m+lcdP.pixels['x']):
pixmap.draw_point(char_gc, ii, jj)
return pixmap
Ho pensato che forse era il modo in cui mi è stato l'assegnazione dei colori. Come si vede nel frammento, ho usato proprio colormap del contesto grafico. Ho provato diverse mappe di colore, essendo questo l'ultimo. Ho anche provato un colore non allocato. Si noti la white_gc e contesti grafici black_gc. Quando uso coloro che sono in grado di attingere nero su sfondo bianco fine. In caso contrario (con un contesto creato) di tutto nero, fg e bg. Quando cambio colore di primo piano del bianco, si tratta sempre di black out.
Ecco l'output. Notate il colore non cambia molto. Direi che non è cambiata, o almeno non abbastanza per importa visivamente.
Before 6 174 60340
After 5 174 60340
Ecco come ho allocare i colori.
def NewColor(red=0, green=0, blue=0, text=None, colormap=None):
if text == None:
c = gtk.gdk.Color(red, green, blue)
else:
c = gtk.gdk.color_parse(text)
if colormap == None:
colormap = gtk.gdk.colormap_get_system()
colormap.alloc_color(c)
return c
Soluzione
Ho avuto qualche problema con Drawable e GC in passato. Questa risposta mi ha iniziato sulla strada per una soluzione. Ecco un rapido esempio che utilizza un gc colore personalizzato di trarre alcune piazze:
import gtk
square_sz = 20
pixmap = None
colour = "#FF0000"
gc = None
def configure_event( widget, event):
global pixmap
x, y, width, height = widget.get_allocation()
pixmap = gtk.gdk.Pixmap(widget.window, width, height)
white_gc = widget.get_style().white_gc
pixmap.draw_rectangle(white_gc, True, 0, 0, width, height)
return True
def expose_event(widget, event):
global pixmap
if pixmap:
x , y, w, h = event.area
drawable_gc = widget.get_style().fg_gc[gtk.STATE_NORMAL]
widget.window.draw_drawable(drawable_gc, pixmap, x, y, x, y, w, h)
return False
def button_press_event(widget, event):
global pixmap, square_sz, gc, colour
if event.button == 1 and pixmap:
x = int(event.x / square_sz) * square_sz
y = int(event.y / square_sz) * square_sz
if not gc:
gc = widget.window.new_gc()
gc.set_rgb_fg_color(gtk.gdk.color_parse(colour))
pixmap.draw_rectangle(gc, True, x, y, square_sz, square_sz)
widget.queue_draw_area(x, y, square_sz, square_sz)
return True
if __name__ == "__main__":
da = gtk.DrawingArea()
da.set_size_request(square_sz*20, square_sz*20)
da.connect("expose_event", expose_event)
da.connect("configure_event", configure_event)
da.connect("button_press_event", button_press_event)
da.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK)
w = gtk.Window()
w.add(da)
w.show_all()
w.connect("destroy", lambda w: gtk.main_quit())
gtk.main()
Speranza che aiuta.
Altri suggerimenti
Il problema è che la funzione NewColor()
restituisce un c
colore non allocato. colormap.alloc_color()
restituisce un gtk.gdk.Color
che è il colore assegnato. Per sistemare le cose l'ultima riga in NewColor()
dovrebbe essere:
return colormap.alloc_color(c)