我想使用Python 2.6和gtk +制作横向滚动文本框(“自动收报机”显示)(按照Centos 6.3)。
我做了一个定时器驱动的例程,它接受一个文本字符串并重复打印它,同时增加打印窗口中的偏移量。这可行,但似乎比我想要的处理器密集程度稍高。
有没有一种方法可以在某种程度上使用块移动加速,并且可以在大多数GPU上使用“blitting”硬件,而不是以增量偏移完全重复打印字符串?我想知道字符串是否可以打印到某种类型的像素缓冲区,然后相关部分可以“blitted”到屏幕内存?任何意见或经验将不胜感激。
我的目标硬件基于Intel 945GME。
答案 0 :(得分: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()