1

我想使用 Python 2.6 和 gtk+(根据 Centos 6.3)制作一个横向滚动的文本框(“自动收报机”显示)。

我制作了一个计时器驱动的例程,它接受一个文本字符串并重复打印它,同时增加打印窗口中的偏移量。这行得通,但似乎比我想要的处理器密集程度略高。

而不是以递增的偏移量完全重复地打印字符串 - 有没有办法以某种方式使用块移动加速并从大多数 GPU 上使用“blitting”硬件中受益?我想知道字符串是否可以打印到某种类型的像素缓冲区,然后相关部分可以“blitted”到屏幕内存?任何意见或经验将不胜感激。

我的目标硬件是基于 Intel 945GME 的。

4

1 回答 1

1

我在某些代码上的最佳尝试(到目前为止)如下。不,我在这些事情上不是很有经验,这几乎不是“示例代码” - 但我认为我应该分享“我不知道”

import gtk
import pango
import glib
import cairo


class Scroller(gtk.DrawingArea):
    def __init__(self, TextString):
        super(Scroller, self).__init__()
        self.offset_x = 0
        self.offset_y = 0
        self.screen_x = 1000
        self.screen_y = 0
# process the y=text string to make an image of it
        self.p = self.create_pango_layout(TextString)
        self.p.set_font_description(pango.FontDescription('Sans 36'))
        attr = pango.AttrList()
        attr.insert(pango.AttrForeground(60535, 60535, 0, 0, -1))
        self.p.set_attributes(attr)
        self.p.set_width(-1)
        self.text_width, self.text_height = self.p.get_size()
        self.text_width = self.text_width / pango.SCALE
        self.text_height = self.text_height / pango.SCALE

#create a pixmap with the data of the pixbuf
        self.pixelbuffer = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8,  self.text_width, self.text_height)
        self.pixelbuffer.fill(0) 
        pixmap,_ = self.pixelbuffer.render_pixmap_and_mask()
        self.pgc = pixmap.new_gc()

        pixmap.draw_layout(self.pgc, 0, 0, self.p)
# now copy image back to pixelbuiffer
        self.pixelbuffer.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, -1, -1)

    def initscreen(self):
        print "screen initialised"
        self.window.clear()
        self.update_display()

    def update_display(self):
        offset = -1
# this scrolls the text sideways
        self.screen_x = self.screen_x + offset
        self.window.draw_pixbuf(self.pgc, self.pixelbuffer, self.offset_x, self.offset_y, self.screen_x, self.screen_y)
# and repeat until bored.
        if self.screen_x == -self.text_width: 
            self.screen_x =1000
        return True


# Main program 
class PyApp(gtk.Window): 
    def __init__(self):
        super(PyApp, self).__init__()

        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(0, 0, 0))
        self.connect("destroy", gtk.main_quit)
        self.set_title("side scroller")


        scroller_screen=Scroller('Breaking News - scrolling text works! More soon....Olympic Games in London are highly entertaining. No more please!')
        self.add(scroller_screen)       
        self.set_size_request(1000, 100)
        self.show_all()
        scroller_screen.initscreen
# set a 20 millisec scroll refreash timer
        glib.timeout_add(20, scroller_screen.update_display)

PyApp()

gtk.main()
于 2012-08-09T12:59:23.463 回答