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