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
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# Copyright 2007 Benjamin M. Schwartz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import gtk
import gtk.gdk
import gobject
import arange
import locale
from gettext import gettext
class TempToolbar(gtk.Toolbar):
_speed = 0
def __init__(self):
gtk.Toolbar.__init__(self)
temp_label = gtk.Label(gettext("Temperature (C): "))
self._temp_field = gtk.Entry()
self._temp_field.set_max_length(6)
self._temp_field.set_width_chars(6)
self._temp_field.connect("changed", self._update_cb)
temp_group = gtk.HBox()
temp_group.pack_start(temp_label, expand=False, fill=False)
temp_group.pack_end(self._temp_field, expand=False,fill=False)
humid_label = gtk.Label(gettext("Relative Humidity (%): "))
self._humid_field = gtk.Entry()
self._humid_field.set_max_length(5)
self._humid_field.set_width_chars(5)
self._humid_field.connect("changed", self._update_cb)
humid_group = gtk.HBox()
humid_group.pack_start(humid_label, expand=False, fill=False)
humid_group.pack_end(self._humid_field, expand=False, fill=False)
result_label = gtk.Label(gettext("Speed of Sound (m/s): "))
self._result = gtk.Label()
result_group = gtk.HBox()
result_group.pack_start(result_label, expand=False, fill=False)
result_group.pack_end(self._result, expand=False, fill=False)
bigbox = gtk.HBox()
bigbox.pack_start(temp_group, expand=False, fill=False)
bigbox.pack_start(humid_group, expand=True, fill=False)
bigbox.pack_end(result_group, expand=False, fill=False)
self.set_temp(25)
self.set_humid(60)
self.update_speed()
tool_item = gtk.ToolItem()
tool_item.add(bigbox)
tool_item.set_expand(True)
self.insert(tool_item, 0)
tool_item.show()
def get_temp(self):
try:
t = locale.atof(self._temp_field.get_text())
except:
t = None
finally:
return t
def set_temp(self, t):
try:
self._temp_field.set_text(locale.str(t))
return True
except:
return False
def get_humid(self):
try:
t = locale.atof(self._humid_field.get_text())
except:
t = None
finally:
return t
def set_humid(self, h):
try:
self._humid_field.set_text(locale.str(max(0,min(100,h))))
return True
except:
return False
def get_speed(self):
return self._speed
def _set_speed(self, s):
self._speed = s
try:
self._result.set_text(locale.format('%.2f',s))
return True
except:
return False
def _update_cb(self, widget=None):
gobject.idle_add(self.update_speed)
def update_speed(self):
t = self.get_temp()
h = self.get_humid()
if (t is not None) and (h is not None):
s = arange.speed_of_sound(t, h/100)
self._set_speed(s)
return False
|