0

I'm trying to made a little graphic with a nice colour gradient and a text

the colour gradient works now fine (only testing so code still messy) but the text doesn't show up up

can someone please tell me what's wrong with my code?

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 24 13:31:18 2012

@author: ksr
"""
import os
import scipy
import pickle
import shutil
import matplotlib.pyplot as plt
from os.path import join as opj
from os import getcwd as cwd
from os import listdir as ld
import wx

def dec2hex(n):
"""return the hexadecimal string representation of integer n"""
return "%X" % n

def hex2dec(s):
    """return the integer value of a hexadecimal string s"""
    return int(s, 16)

def main(text):

    app         = wx.PySimpleApp()

    # Bitmap und DC (Device Context) erstellen
    bitmap      = wx.EmptyBitmap(width = 500, height = 500)
    dc          = wx.MemoryDC(bitmap) # Dieser DC zeichnet nur im Speicher

    # Hintergrund gelb ausmalen
    dc.SetBackground(wx.Brush('#FFFFFF', wx.SOLID))
    dc.Clear()

    # GraphicsContext erstellen (damit sehen die Bilder besser aus)
    gc = wx.GraphicsContext_Create(dc)

    x = 0
    y = 0
    far = 0
    z = 0
    for i in range(0,2001):

        if z%5 == 0 and i < 1000:
            far += 1
            z = 0
        elif z%5 == 0 and i > 1000:
            far -= 1
            z = 0
        farbe = dec2hex(far)


        # Grünen Stift zuweisen
        pen = wx.Pen("#%s0000"%(str(farbe.rjust(2,'0'))), 2, wx.SOLID)#,str(farbe.rjust(2,'0'))
        gc.SetPen(pen)

        # Zwei horizontale Linien zeichnen
        gc.DrawLines(((x, y), (256, 256)))

        print x,y,farbe.rjust(2,'0'),i

        if x < 500 and y == 0:
            x+=1

        elif x == 500 and y < 500: 
            y+=1

        elif y == 500 and x > 0:
            x-=1

        elif x == 0 and y > 0:
            y-=1
        z+=1    

    gc.DrawText('Hello',100,100)    

    # Bitmap vom DC abtrennen und als PNG speichern
    dc.SelectObject(wx.NullBitmap)
    bitmap.SaveFile("new_image.png", wx.BITMAP_TYPE_PNG)


if __name__ == "__main__":
    main()
Zeugma
  • 31,231
  • 9
  • 69
  • 81
Christian Kaiser
  • 160
  • 1
  • 1
  • 10

1 Answers1

0

When I run your code, I got an error as there is not Font defined, I don't know if you experience the same issue, but by adding any SetFont statement like gc.SetFont(wx.Font(22, wx.SWISS, wx.NORMAL, wx.BOLD)) before DrawText your code is working fine.

By the way, I put 300, 300 as coordinates to draw your 'Hello' text as in 100, 100 it is a little black on black and unreadable.

Zeugma
  • 31,231
  • 9
  • 69
  • 81
  • Thank you very much! I never got an Error like you. Its a little bit confusing so could you tell please me which programming environment do you use ?( I'm using Spyder under ubuntu 10.04) – Christian Kaiser Apr 27 '12 at 10:21
  • I use [PyScripter](http://code.google.com/p/pyscripter/) on Windows, running remote python engine for Wx. – Zeugma Apr 27 '12 at 10:48
  • I would be surprised you didn't get any Traceback somehow. Isn't it displayed in you IDE output somewhere? It could be caught, displayed, and then getting wx keeping running to the next instruction. – Zeugma Apr 27 '12 at 10:51