PyGTK gráficos contextos e cores alocação
Pergunta
Eu procurei sobre isso, mas nada tem o que eu estou procurando.
http://www.mail-archive.com/pygtk@daa.com.au/msg10529 .html - Ninguém respondeu ele. Este é exatamente o que estou experimentando. Quando eu definir o plano em um contexto de gráficos, ele não parece realmente mudança.
Eu estive com o tutorial e FAQ, mas não dizer muito. Eles quer apenas usar os contextos preto e branco ou eles dão-lhe links quebrados. Eu estou à esquerda pensar talvez seja um bug. Mas o garoto em mim diz que eu apenas estou faltando alguma coisa e eu continuo ignorando o fato de que eu tenho uma alternativa de trabalho. Isso seria melhor ainda. E quanto mais eu chegar a esta, mais eu vou precisar desses contextos e cores.
Aqui está o meu trecho de código.
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
Eu pensei que talvez fosse o jeito que eu estava alocando as cores. Como você pode ver no trecho, eu usei próprio mapa de cores do contexto gráfico. Eu tentei diferentes mapas de cores, sendo este o mais tardar. Eu até tentei uma cor não alocado. Observe os white_gc e gráficos black_gc contextos. Quando eu uso aqueles que eu sou capaz de desenhar em preto em um fundo branco fino. Caso contrário (com um contexto criado) preto, fg de tudo e bg. Quando eu mudar de cor de primeiro plano do branco, que sempre sai negro.
Aqui está a saída. Observe a cor não muda muito. Eu diria que não mudou, ou pelo menos não o suficiente para importa visualmente.
Before 6 174 60340
After 5 174 60340
Aqui está como eu alocar as cores.
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
Solução
Eu tive alguns problemas com Drawable e GC no passado. Esta resposta me iniciou no caminho para uma solução. Aqui está um exemplo rápido que usa um gc cor personalizada para desenhar alguns quadrados:
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()
Hope isso ajuda.
Outras dicas
O problema é que a função NewColor()
está retornando um c
cor não alocado. colormap.alloc_color()
retorna um gtk.gdk.Color
que é a cor alocado. Para corrigir as coisas a última linha no NewColor()
deve ser:
return colormap.alloc_color(c)