@shellex说: 额....

自己做一个cairo clock

由于手头工作的原因,我要接触cairo这个矢量图形库,因为它的免费高效跨平台。如果你现在使用的Firefox 3浏览器,那么恭喜你,Cairo正在为你工作。当然了,GTK和Gnome这样的项目也采用了Cairo。

Cairo:http://www.cairographics.org/

下面的内容你可能需要一些GTK和python的前置知识。还有Cairo本身.

import gtk
import pygtk
import cairo
import math
import time
import gobject

class Clock(gtk.DrawingArea):
	def progress_timeout(self, object):
		width, height = self.get_parent().size_request()
		#Adds region to the update area for window
		object.window.invalidate_rect((0, 0, width, height), False)
		return True

	def __init__(self):
		gtk.DrawingArea.__init__(self)
		self.connect("expose_event", self.expose_cb)
		self.timer = gobject.timeout_add (300, self.progress_timeout, self)

	def expose_cb(self, widget, event):
		self.context = widget.window.cairo_create()
		        # set a clip region for the expose event
		self.context.rectangle(event.area.x, event.area.y,
                               event.area.width, event.area.height)
		self.context.clip()
		self.repaint(self.context)

		return False

	def scroll_event_cb(self, widget, event):
		if self.get_toplevel().get_opacity() < 0 or self.get_toplevel().get_opacity() > 1:
			return
		if event.direction == gtk.gdk.SCROLL_UP:
			self.get_toplevel().set_opacity(self.get_toplevel().get_opacity() + 0.1)
		else:
			self.get_toplevel().set_opacity(self.get_toplevel().get_opacity() - 0.1)

	def repaint(self, context):
		width, height = self.get_parent().size_request()
		radius = min(width, height) / 2;

		#draw clock pane
		context.arc(radius, radius,
					radius, 0, 2 * math.pi)
		context.set_source_rgb(1, 1, 1)
		context.fill_preserve()
		context.set_source_rgb(0, 0, 0)
		context.stroke()

		for i in range(0, 12):
			arc = 2*math.pi / 12 * i
			context.arc(math.cos(arc) * radius * 0.98 + radius, math.sin(arc) * radius * 0.98 + radius, 2, 0, 2 * math.pi)
			context.fill()
			context.stroke()

		#get current local time
		hours = time.localtime().tm_hour
		minutes = time.localtime().tm_min
		secs = time.localtime().tm_sec

		sec_arc = (2 * math.pi / 60) * secs - 0.5 * math.pi
		minute_arc = (2 * math.pi / 60) * minutes - 0.5 * math.pi

		if hours > 12:
			hours = hours - 12
		hour_arc = (2 * math.pi / 12) * (hours + (minutes + 0.0) / 60) - 0.5 * math.pi

		#draw points
		point_arc = [(hour_arc, 0.6), (minute_arc, 0.7), (sec_arc, 0.8)]
		for arc, length in point_arc:
			context.move_to(radius, radius)
			context.line_to(radius * math.cos(arc) * length + radius, radius * math.sin(arc) * length + radius )
			context.stroke()

		#draw digital clock
		buf = "%2d:%2d:%2d" % (hours, minutes, secs)
		context.select_font_face("Sans")
		context.set_font_size(20.0)
		xbearing, ybearing, width, height, xadvance, yadvance = context.text_extents(buf)
		context.move_to(radius - width / 2, radius + height * 3)
		context.show_text(buf)
		context.stroke()

		#draw my name
		name_buf = 'shellex (sxnsx.com)'
		context.select_font_face("Sans")
		context.set_font_size(10.0)
		xbearing, ybearing, width, height, xadvance, yadvance = context.text_extents(name_buf)
		context.move_to(radius - width / 2, radius - height * 2)
		context.show_text(name_buf)
		context.stroke()

def main():
	window = gtk.Window()
	#window.set_size_request(200, 200)
	clock = Clock()
	clock.set_size_request(200, 200)

	window.set_title("Cairo Clock")
	window.connect("destroy", gtk.main_quit)
	event_box = gtk.EventBox()
	event_box.connect("scroll_event", clock.scroll_event_cb)
	event_box.add(clock)
	window.add(event_box)
	window.show_all()

	gtk.main()

main()
  1. On July 13, 2008 at 4:53 pm

    ya, 这排版,对于python这样的语言简直是噩梦一样。

    Notify

Leave a Reply