inicio mail me! sindicaci;ón

自己做一个cairo clock

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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()
Tags: , ,

Related posts(相关文章)

Gravatar
沙发: (Using Mozilla Firefox Mozilla Firefox 3.0 on Linux Linux)

shellex said:

July 13, 2008 @ 4:53 pm

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


引用此人(Quote)

RSS feed for comments on this post · TrackBack URI

Leave a Comment