【问题标题】:Use wxpython DrawText function to add text in a Bitmap使用 wxpython DrawText 函数在位图中添加文本
【发布时间】:2012-04-27 09:19:02
【问题描述】:

我正在尝试用漂亮的颜色渐变和文字制作一个小图形

颜色渐变现在可以正常工作(仅测试所以代码仍然很乱) 但是文字没有显示出来

谁能告诉我我的代码有什么问题?

#!/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()

【问题讨论】:

    标签: python bitmap wxpython draw drawtext


    【解决方案1】:

    当我运行你的代码时,我收到了一个错误,因为没有定义字体,我不知道你是否遇到同样的问题,但是在你的代码之前添加任何 SetFont 语句,如 gc.SetFont(wx.Font(22, wx.SWISS, wx.NORMAL, wx.BOLD)) 之前 DrawText工作正常。

    顺便说一句,我把300, 300 作为坐标来绘制你的“你好”文本,就像100, 100 一样,它是黑底黑字,不可读。

    【讨论】:

    • 非常感谢!我从来没有像你这样的错误。这有点令人困惑,所以你能告诉我你使用哪种编程环境吗?(我在 ubuntu 10.04 下使用 Spyder)
    • 我在 Windows 上使用PyScripter,为 Wx 运行远程 python 引擎。
    • 我会很惊讶您没有以某种方式获得任何 Traceback。它不是显示在您的IDE输出中吗?它可以被捕获、显示,然后让 wx 继续运行到下一条指令。
    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多