initial revision of unit_converter

git-svn-id: http://www.neo1973-germany.de/svn@38 46df4e5c-bc4e-4628-a0fc-830ba316316d
main
PatrickBeck 16 years ago
parent f5382d461f
commit b1217d34a7

@ -0,0 +1,306 @@
images {
image: "bg.png" COMP;
image: "phone-asleep.png" COMP;
image: "power.png" COMP;
image: "location-off.png" COMP;
image: "hotornot.png" COMP;
image: "maybe.png" COMP;
image: "yes.png" COMP;
image: "no.png" COMP;
}
collections {
group {
name: "main";
min: 480 640;
script {
public selected = 0;
public pulsing = 0;
public stop_pulsing_timer_id = 0;
const Float:pulse_timeout = 10.0;
public unselect() {
if (get_int(selected) == 0)
return;
run_program(get_int(selected));
set_int(selected, 0);
}
public stop_pulsing() {
if (get_int(pulsing) == 0)
return;
set_state(get_int(pulsing), "default", 0.0);
set_int(pulsing, 0);
if (get_int(stop_pulsing_timer_id) != 0) {
cancel_timer(get_int(stop_pulsing_timer_id));
set_int(stop_pulsing_timer_id, 0);
}
}
public stop_pulsing_cb(val) {
stop_pulsing();
return 0;
}
public item_alpha = 0;
public item_timer = 0;
public alpha_change() {
custom_state(PART:"hotornot_area", "default", 0.0);
set_state_val(PART:"hotornot_area", STATE_COLOR, 255, 255, 255, get_int(item_alpha));
set_state(PART:"hotornot_area", "custom", 0.0);
set_int(item_alpha, get_int(item_alpha) +10);
timer(0.1, "alpha_change", 1);
}
public y_position = 0;
public scroll_down() {
new x1;
new Float: y1;
new x2;
new Float: y2;
custom_state(PART:"scrolling_area", "default", 0.0);
get_state_val(PART:"scrolling_area", STATE_REL1, x1, y1);
get_state_val(PART:"scrolling_area", STATE_REL2, x2, y2);
y1 = y1 + 0.01;
y2 = y2 + 0.01;
set_state_val(PART:"scrolling_area", STATE_REL1, x1, y1);
set_state_val(PART:"scrolling_area", STATE_REL2, x2, y2);
set_state(PART:"scrolling_area", "custom", 0.0);
timer(0.1, "scroll_down", 1);
}
}
parts {
part {
name: "bg";
type: IMAGE;
mouse_events: 0;
description {
state: "default" 0.0;
image { normal: "bg.png"; }
fill {
size {relative: 0 0;offset: 160 160;}
}
}
}
part {
name: "scrolling_area";
type: RECT;
mouse_events: 0;
description {
state: "default" 0.0;
rel1 { relative: 0 0;}
rel2 { relative: 1 1;}
color: 0 0 0 0;
}
}
#define ICON(part_name, pos, part_label) \
part { \
name: part_name"_area"; \
type: RECT; \
mouse_events: 0; \
description { \
state: "default" 0.0; \
rel1 { relative: 0 0.25*pos; offset: 5 5; to: "scrolling_area"; } \
rel2 { relative: 1 0.25*(pos+1); offset: -5 -5; to: "scrolling_area"; } \
color: 255 255 255 128; \
} \
description { \
state: "up" 0.0; \
inherit: "default" 0.0; \
color: 255 255 255 64; \
} \
} \
part { \
name: part_name; \
type: IMAGE; \
mouse_events: 0; \
description { \
state: "default" 0.0; \
aspect: 1.0 1.0;\
align: 0.0 0.0; \
rel1 { relative: 0 0.25*pos; offset: 10 10; to: "scrolling_area"; } \
rel2 { relative: 0 0.25*(pos+1); offset: 0 -10; to: "scrolling_area"; } \
image { normal: part_name".png"; } \
} \
description { \
state: "up" 0.0; \
inherit: "default" 0.0; \
color: 255 255 255 128; \
} \
} \
part { \
name: part_name"_label"; \
type: TEXT; \
effect: SHADOW; \
mouse_events: 0; \
description { \
state: "default" 0.0; \
rel1 { relative: 1 0; to: part_name; } \
rel2 { relative: 1 0.25; to_y: part_name; to_x: "scrolling_area"; } \
align: 0.0 0.0; \
color: 255 255 255 255; \
color2: 0 0 0 255; \
color3: 0 0 0 255; \
text { \
font: "Sans"; \
size: 18; \
text: part_label; \
min: 1 1; \
fit: 1 1; \
align: 0 0; \
} \
} \
description { \
state: "up" 0.0; \
inherit: "default" 0.0; \
color: 255 255 255 255; \
color2: 0 0 0 255; \
color3: 0 0 0 255; \
} \
}
ICON("hotornot", 0, "Hotornot")
ICON("location-off", 1, "GPS")
ICON("power", 2, "power mgmt")
ICON("phone-asleep", 3, "telefon")
part {
name: "up";
type: RECT;
mouse_events: 1;
description {
state: "default" 0.0;
rel1 { relative: 0 0;}
rel2 { relative: 1 0.25;}
color: 0 0 0 0;
}
}
part {
name: "down";
type: RECT;
mouse_events: 1;
description {
state: "default" 0.0;
rel1 { relative: 0 0.5;}
rel2 { relative: 1 0.75;}
color: 0 0 0 0;
}
}
part {
name: "maybe";
type: IMAGE;
mouse_events: 0;
description {
state: "default" 0.0;
aspect: 1.0 1.0;
align: 0.5 0.0;
rel1 { relative: 0.125 0.78;}
rel2 { relative: 0.125 1;}
image { normal: "maybe.png"; }
}
}
part {
name: "yes";
type: IMAGE;
mouse_events: 0;
description {
state: "default" 0.0;
aspect: 1.0 1.0;
align: 0.5 0.0;
rel1 { relative: 0.625 0.78;}
rel2 { relative: 0.625 1;}
image { normal: "yes.png"; }
}
}
part {
name: "no";
type: IMAGE;
mouse_events: 0;
description {
state: "default" 0.0;
aspect: 1.0 1.0;
align: 0.5 0.0;
rel1 { relative: 0.875 0.78;}
rel2 { relative: 0.875 1;}
image { normal: "no.png"; }
}
}
part {
name: "Dinge";
type: TEXT;
effect: SHADOW;
mouse_events: 0;
description {
state: "default" 0.0;
rel1 { relative: 0 0;}
rel2 { relative: 1 0.125;}
align: 0.0 0.0;
color: 255 187 0 255;
color2: 0 0 0 255;
color3: 0 0 0 255;
text {
font: "Sans";
size: 18;
text: "Dinge";
min: 1 1;
fit: 1 1;
align: 0.5 0;
}
}
}
programs {
program {
name: "scrollup";
signal: "mouse,clicked,1";
source: "up";
action: STATE_SET up5 0.0;
target: "hotornot_area";
transition: LINEAR 0.2;
script {
// alpha_change();
/*
new x1;
new Float: y1;
new x2;
new Float: y2;
custom_state(PART:"scrolling_area", "default", 0.0);
get_state_val(PART:"scrolling_area", STATE_REL1, x1, y1);
get_state_val(PART:"scrolling_area", STATE_REL2, x2, y2);
y1 = y1 - 0.25;
y2 = y2 - 0.25;
set_state_val(PART:"scrolling_area", STATE_REL1, x1, y1);
set_state_val(PART:"scrolling_area", STATE_REL2, x2, y2);
set_state(PART:"scrolling_area", "custom", 0.0);*/
}
}
program {
name: "scrolldown";
signal: "mouse,clicked,1";
source: "down";
script {
scroll_down();
}
}
}
}
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

@ -0,0 +1,233 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# unit_converter - Converts between different units
# version 0.01
# by Patrick Beck
import pygtk
pygtk.require('2.0')
import gtk
import xml.dom.minidom
unitfile = 'unit.xml' # path to the unit file
file = xml.dom.minidom.parse(unitfile) # parse the xml file
class unitconvert(object):
toactive = 0 # status variable will be set to 1, when the 'to' field is the last activated field
fromactive = 0 # the same, only for the 'from' field
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def fromentry_changed(self, widget):
self.fromactive = 1
self.toactive = 0
def toentry_changed(self, widget):
self.toactive = 1
self.fromactive = 0
def on_catcombo_changed(self, widget):
self.create_menu()
def category_isset(self): # get the selected category
categoryname = self.catcombo.get_active_text()
return categoryname
def unit_from_isset(self): # get the selected unit from the 'from' field
unit_from = self.fromcombo.get_active_text()
return unit_from
def unit_to_isset(self): # get the selected unit from the 'to' field
unit_to = self.tocombo.get_active_text()
return unit_to
def number_from_isset(self): # get the data from the 'from' entry field
getnumber_from = self.fromentry.get_text()
number_from = float(getnumber_from.replace(',','.')) # replace the comma with a dot for the internal calculation
return number_from
def number_to_isset(self): # get the data for the 'to' entry field
getnumber_to = self.toentry.get_text()
number_to = float(getnumber_to.replace(',','.'))
return number_to
def fromentry_set(self, printresult): # sets the result in the target field
self.fromentry.set_text(printresult)
def toentry_set(self, printresult): # the same as above
self.toentry.set_text(printresult)
def categorymenu(self): # creates the data for the category menu from the xml file
category = []
for categoryname in file.getElementsByTagName('category'): # browses through the elements in the file
category.append(categoryname.getAttribute('name')) # append the name to the category list
return category
def unitmenu(self): # creates the data for the unit menu
unit = []
set_category = self.category_isset() # checks for the active category to add this units
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == set_category: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
unit.append(units.getAttribute('name')) # appends the units from the category into the unit list
return unit
def create_menu(self): # fill the gui with the data from categorymenu and unitmenu
if self.category_isset() == None: # if no category menu exists
for categorys in self.categorymenu():
self.catcombo.append_text(categorys) # append the categorys to the menu
modelfrom = self.fromcombo.get_model() # get the data from the fromcombo field ...
modelfrom.clear() # ... and delete them for the new data
modelto = self.tocombo.get_model() # the same as fromcombo
modelto.clear()
for units in self.unitmenu():
self.fromcombo.append_text(units) # filling the unit menus
self.tocombo.append_text(units)
def convert(self, widget):
category_isset = self.category_isset() # get the active category from the gui
unit_to = self.unit_to_isset() # gets the active unit from the 'to' field
unit_from = self.unit_from_isset() # gets the same for the 'from' field
if self.toactive == 1:
number_to = self.number_to_isset()
if self.fromactive == 1:
number_from = self.number_from_isset()
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == category_isset: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
if units.getAttribute('name') == unit_from: # the name from the unit in the category musst set in the to field
if self.fromactive == 1: # when active gets the to_ref field
toref = units.getAttribute('to_ref')
else:
fromref = units.getAttribute('from_ref') # else the from_ref field
if units.getAttribute('name') == unit_to:
if self.fromactive == 1:
fromref = units.getAttribute('from_ref')
else:
toref = units.getAttribute('to_ref')
if self.fromactive == 1: # select the basis for the calculation => when the from field was changed use the data from this field
number = number_from
if self.toactive == 1:
number = number_to
result = eval(toref) # execute the formular in the toref field and safe it to result
endresult = eval(fromref) # convert from the refunit to the target unit
printresult = str(endresult).replace('.',',') # for the better readability replace the dot with a comma
if self.fromactive == 1:
self.toentry_set(printresult) # sets the result into the 'from' field
else:
self.fromentry_set(printresult) # same as above for the 'to' field
def clear(self, widget): # clears the entry fields
self.fromentry.set_text('')
self.toentry.set_text('')
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title('Unit Converter')
self.window.connect('delete_event', self.delete_event)
self.window.connect('destroy', self.destroy)
# create a box in the dimensions of the window and add it to them
self.box = gtk.VBox(False,2)
self.window.add(self.box)
# create a Hbox for the category and add them to self.box
self.catbox = gtk.HBox(True,2)
# frame for the category box
self.catframe = gtk.Frame('Category')
self.box.pack_start(self.catframe, False, False, 0)
# a combobox for the categorys
self.catcombo = gtk.combo_box_new_text()
self.catcombo.connect('changed', self.on_catcombo_changed)
self.catbox.pack_start(self.catcombo, False, False, 0)
self.catframe.add(self.catbox)
#create a vbox for the From units
self.frombox = gtk.VBox(False,2)
#frame for the from box
self.fromframe = gtk.Frame('Convert from ...')
self.box.pack_start(self.fromframe, False, False, 0)
self.fromframe.add(self.frombox)
#create a entry for the 'Convert from' field
self.fromentry = gtk.Entry()
self.fromentry.set_text('Convert from ...')
self.fromentry.connect('changed', self.fromentry_changed)
self.frombox.pack_start(self.fromentry, False, False, 0)
#a combobox for the from unit
self.fromcombo = gtk.combo_box_new_text()
self.frombox.pack_start(self.fromcombo, False, False, 0)
#create a vbox for the To units
self.tobox = gtk.VBox(False,2)
#frame for the to box
self.toframe = gtk.Frame('Convert to ...')
self.box.pack_start(self.toframe, False, False, 0)
self.toframe.add(self.tobox)
#create a entry for the 'Convert to' field
self.toentry = gtk.Entry()
self.toentry.set_text('Convert to ...')
self.toentry.connect('changed', self.toentry_changed)
self.tobox.pack_start(self.toentry, False, False, 0)
#a combobox for the to unit
self.tocombo = gtk.combo_box_new_text()
self.tobox.pack_start(self.tocombo, False, False , 0)
#create a box for the go and clear button
self.sendbox = gtk.HBox(True,2)
self.box.pack_end(self.sendbox, False, False, 0)
self.gobutton = gtk.Button('Go')
self.sendbox.pack_start(self.gobutton, True, True, 0)
self.gobutton.connect('clicked', self.convert)
self.clearbutton = gtk.Button('Clear')
self.sendbox.pack_start(self.clearbutton, True, True, 0)
self.clearbutton.connect('clicked', self.clear)
# Display all elements
self.box.show()
self.catbox.show()
self.catcombo.show()
self.catframe.show()
self.fromentry.show()
self.fromcombo.show()
self.fromframe.show()
self.frombox.show()
self.toentry.show()
self.tocombo.show()
self.toframe.show()
self.tobox.show()
self.sendbox.show()
self.clearbutton.show()
self.gobutton.show()
self.window.show()
def main():
gtk.main()
return 0
if __name__ == '__main__':
unitconvert().create_menu()
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

@ -0,0 +1,10 @@
x = 255
list = []
while x != 0:
x2 = x%2
list.append(x2)
x = x/2
print list
for i in reversed(list):
print i,

@ -0,0 +1,305 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# unit_converter - Converts between different units
# version 0.1
# 2008/02/16
# by Patrick Beck
import ecore
import ecore.evas
import edje
import xml.dom.minidom
from sys import exit
try:
unitfile = 'unit.xml' # path to the unit file
file = xml.dom.minidom.parse(unitfile) # parse the xml file
except:
print 'The unitfile can not be opened or found'
exit()
class unitconvert(object):
toactive = 0 # status variable will be set to 1, when the 'to' field is the last activated field
fromactive = 0 # the same, only for the 'from' field
def fromentry_changed(self, widget):
self.fromactive = 1
self.toactive = 0
def toentry_changed(self, widget):
self.toactive = 1
self.fromactive = 0
def on_catcombo_changed(self, widget):
self.create_menu()
def category_isset(self): # get the selected category
categoryname = self.catcombo.get_active_text()
return categoryname
def unit_from_isset(self): # get the selected unit from the 'from' field
unit_from = self.fromcombo.get_active_text()
return unit_from
def unit_to_isset(self): # get the selected unit from the 'to' field
unit_to = self.tocombo.get_active_text()
return unit_to
def number_from_isset(self): # get the data from the 'from' entry field
getnumber_from = self.fromentry.get_text()
try: # Test if the entry is a number
number_from = float(getnumber_from.replace(',','.')) # replace the comma with a dot for the internal calculation
return number_from
except:
return 'nonnumber'
def number_to_isset(self): # get the data for the 'to' entry field
getnumber_to = self.toentry.get_text()
try: # Test if the entry is a number
number_to = float(getnumber_to.replace(',','.'))
return number_to
except:
return 'nonnumber'
def fromentry_set(self, printresult): # sets the result in the target field
self.fromentry.set_text(printresult)
def toentry_set(self, printresult): # the same as above
self.toentry.set_text(printresult)
def categorymenu(self): # creates the data for the category menu from the xml file
category = []
for categoryname in file.getElementsByTagName('category'): # browses through the elements in the file
category.append(categoryname.getAttribute('name')) # append the name to the category list
return category
def unitmenu(self): # creates the data for the unit menu
unit = []
set_category = self.category_isset() # checks for the active category to add this units
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == set_category: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
unit.append(units.getAttribute('name')) # appends the units from the category into the unit list
return unit
def create_menu(self): # fill the gui with the data from categorymenu and unitmenu
if self.category_isset() == None: # if no category menu exists
for categorys in self.categorymenu():
self.catcombo.append_text(categorys) # append the categorys to the menu
modelfrom = self.fromcombo.get_model() # get the data from the fromcombo field ...
modelfrom.clear() # ... and delete them for the new data
modelto = self.tocombo.get_model() # the same as fromcombo
modelto.clear()
for units in self.unitmenu():
self.fromcombo.append_text(units) # filling the unit menus
self.tocombo.append_text(units)
def convert(self, widget):
category_isset = self.category_isset() # get the active category from the gui
unit_to = self.unit_to_isset() # gets the active unit from the 'to' field
unit_from = self.unit_from_isset() # gets the same for the 'from' field
# a few tests for the inputs in the combo and entry field
if category_isset == None:
self.fromentry_set('Choose a category')
self.toentry_set('and the units')
elif unit_from == None and unit_to == None:
self.fromentry_set('Choose a unit')
self.toentry_set('Choose a unit')
elif unit_from == None:
self.fromentry_set('Choose a unit')
self.toentry_set('')
elif unit_to == None:
self.toentry_set('Choose a unit')
self.fromentry_set('')
# when the inputs are ok excecute the rest of the program
else:
if self.toactive == 1:
number = self.number_to_isset()
if number == 'nonnumber': # when the entry is not a number print a error
self.toentry_set('Error - put in a number')
else:
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == category_isset: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
if units.getAttribute('name') == unit_from: # the name from the unit in the category musst set in the to field
if self.fromactive == 1: # when active gets the to_ref field
toref = units.getAttribute('to_ref')
else:
fromref = units.getAttribute('from_ref') # else the from_ref field
if units.getAttribute('name') == unit_to:
if self.fromactive == 1:
fromref = units.getAttribute('from_ref')
else:
toref = units.getAttribute('to_ref')
# if self.fromactive == 1: # select the basis for the calculation => when the from field was changed use the data from this field
# number = number_from
# if self.toactive == 1:
# number = number_to
result = eval(toref) # execute the formular in the toref field and safe it to result
endresult = eval(fromref) # convert from the refunit to the target unit
printresult = str(endresult).replace('.',',') # for the better readability replace the dot with a comma
# if self.fromactive == 1:
# self.toentry_set(printresult) # sets the result into the 'from' field
# else:
self.fromentry_set(printresult) # same as above for the 'to' field
else:
number = self.number_from_isset()
if number == 'nonnumber': # when the entry is not a number print a error
self.fromentry_set('Error - put in a number')
else:
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == category_isset: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
if units.getAttribute('name') == unit_from: # the name from the unit in the category musst set in the to field
if self.fromactive == 1: # when active gets the to_ref field
toref = units.getAttribute('to_ref')
else:
fromref = units.getAttribute('from_ref') # else the from_ref field
if units.getAttribute('name') == unit_to:
if self.fromactive == 1:
fromref = units.getAttribute('from_ref')
else:
toref = units.getAttribute('to_ref')
# if self.fromactive == 1: # select the basis for the calculation => when the from field was changed use the data from this field
# number = number_from
# if self.toactive == 1:
# number = number_to
result = eval(toref) # execute the formular in the toref field and safe it to result
endresult = eval(fromref) # convert from the refunit to the target unit
printresult = str(endresult).replace('.',',') # for the better readability replace the dot with a comma
# if self.fromactive == 1:
self.toentry_set(printresult) # sets the result into the 'from' field
# else:
# self.fromentry_set(printresult) # same as above for the 'to' field
def clear(self, widget): # clears the entry fields
self.fromentry.set_text('')
self.toentry.set_text('')
def fromentry_cursor(self, widget, data=None):
self.fromentry.set_text('')
def toentry_cursor(self, widget, data=None):
self.toentry.set_text('')
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title('Unit Converter')
self.window.connect('delete_event', self.delete_event)
self.window.connect('destroy', self.destroy)
# create a box in the dimensions of the window and add it to them
self.box = gtk.VBox(False,2)
self.window.add(self.box)
# create a Hbox for the category and add them to self.box
self.catbox = gtk.HBox(True,2)
# frame for the category box
self.catframe = gtk.Frame('Category')
self.box.pack_start(self.catframe, False, False, 0)
# a combobox for the categorys
self.catcombo = gtk.combo_box_new_text()
self.catcombo.connect('changed', self.on_catcombo_changed)
self.catbox.pack_start(self.catcombo, False, False, 0)
self.catframe.add(self.catbox)
#create a vbox for the From units
self.frombox = gtk.VBox(False,2)
#frame for the from box
self.fromframe = gtk.Frame('Convert from ...')
self.box.pack_start(self.fromframe, False, False, 0)
self.fromframe.add(self.frombox)
#create a entry for the 'Convert from' field
self.fromentry = gtk.Entry()
self.fromentry.set_text('Input a number here ')
self.fromentry.connect('changed', self.fromentry_changed)
self.fromentry.connect('activate', self.convert)
self.fromentry.connect('focus_in_event', self.fromentry_cursor)
self.frombox.pack_start(self.fromentry, False, False, 0)
#a combobox for the from unit
self.fromcombo = gtk.combo_box_new_text()
self.frombox.pack_start(self.fromcombo, False, False, 0)
#create a vbox for the To units
self.tobox = gtk.VBox(False,2)
#frame for the to box
self.toframe = gtk.Frame('Convert to ...')
self.box.pack_start(self.toframe, False, False, 0)
self.toframe.add(self.tobox)
#create a entry for the 'Convert to' field
self.toentry = gtk.Entry()
self.toentry.set_text('... or here')
self.toentry.connect('changed', self.toentry_changed)
self.toentry.connect('activate', self.convert)
self.toentry.connect('focus_in_event', self.toentry_cursor)
self.tobox.pack_start(self.toentry, False, False, 0)
#a combobox for the to unit
self.tocombo = gtk.combo_box_new_text()
self.tobox.pack_start(self.tocombo, False, False , 0)
#create a box for the go and clear button
self.sendbox = gtk.HBox(True,2)
self.box.pack_end(self.sendbox, False, False, 0)
self.gobutton = gtk.Button('Go')
self.sendbox.pack_start(self.gobutton, True, True, 0)
self.gobutton.connect('clicked', self.convert)
self.clearbutton = gtk.Button('Clear')
self.sendbox.pack_start(self.clearbutton, True, True, 0)
self.clearbutton.connect('clicked', self.clear)
# Display all elements
self.box.show()
self.catbox.show()
self.catcombo.show()
self.catframe.show()
self.fromentry.show()
self.fromcombo.show()
self.fromframe.show()
self.frombox.show()
self.toentry.show()
self.tocombo.show()
self.toframe.show()
self.tobox.show()
self.sendbox.show()
self.clearbutton.show()
self.gobutton.show()
self.window.show()
def main():
gtk.main()
return 0
if __name__ == '__main__':
unitconvert().create_menu()
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

@ -0,0 +1,180 @@
collections {
color_classes {
color_class {
name: "empty";
color: 0 0 0 0;
color2: 0 0 0 0;
color3: 0 0 0 0;
}
color_class {
name: "active";
color: 128 128 160 255;
color2: 128 128 128 255;
color3: 128 128 128 255;
}
color_class {
name: "text";
color: 255 170 0 255;
color2: 0 0 0 0;
color3: 0 0 0 0;
}
color_class {
name: "title";
color: 96 96 96 255;
color2: 0 0 0 192;
color3: 0 0 0 32;
}
}
styles {
style {
name: "textblock_style";
base: "font=sans font_size=20 align=left valign=top color=#db0 wrap=word";
tag: "h1" "+ font_size=28";
tag: "/h1" "- \n";
tag: "p" "+";
tag: "/p" "- \n";
tag: "em" "+ style=underline underline_color=#000A underline2_color=#0005";
tag: "/em" "-";
tag: "br" "\n";
}
}
images {
image: "logo_bw.png" COMP;
image: "logo.png" COMP;
image: "plus.png" COMP;
image: "minus.png" COMP;
image: "empty.png" COMP;
}
group {
name: "background";
parts {
// part {
// name: "background_color";
// mouse_events: 0;
// type: RECT;
// description {
// color: 65 110 200 255;
// rel1 {
// relative: 0.0 0.0;
// }
// rel2 {
// relative: 1.0 1.0;
// }
// }
// }
part {
name: "background_image";
mouse_events: 0;
type: IMAGE;
description {
rel1 {
relative: 0.0 0.0;
}
rel2 {
relative: 1.0 1.0;
}
image {
normal: "logo.png";
}
}
}
}
}
group {
name: "foreground";
parts {
part {
name: "plus_image";
type: IMAGE;
description {
rel1 {
relative: 7/8 1;
offset: -50 -110;
}
rel2 {
relative: 7/8 1;
offset: 50 -10;
}
image {
normal: "plus.png";
}
}
}
}
parts {
part {
name: "minus_image";
type: IMAGE;
description {
rel1 {
relative: 1/8 1;
offset: -50 -110;
}
rel2 {
relative: 1/8 1;
offset: 50 -10;
}
image {
normal: "minus.png";
}
}
}
}
}
group {
name: "thing";
min: 480 160;
max: 480 160;
parts {
part {
name:"empty";
type: RECT;
description {
color_class: "active";
rel1 {
relative: 0 0;
}
rel2 {
relative: 1 1;
}
}
}
part {
name:"text";
mouse_events: 0;
type: TEXT;
effect: PLAIN;
description {
// align: 0.5 0.5;
color_class: "title";
rel1 {
relative: 0 0;
}
rel2 {
relative: 1 1;
}
text {
text: "???";
font: "sans.bold";
size: 25;
min: 1 1;
}
}
}
// part {
// name: "select";
// type: IMAGE;
// description {
// rel1 {
// relative: 0 0;
// }
// rel2 {
// relative: 1 1;
// }
// image {
// normal: "minus.png";
// }
// }
// }
}
}

@ -0,0 +1,13 @@
- gui trennen von funktion durch unterschiedliche dateien
2 - unit.xml füllen
- gui verbessern? => e-gui
1 - eval() raushauen
- gutes englisch?
- gpl3 einfügen
- django webseite
- contributors - dank an
- ipkg
- xml auf fehler parsen
# convert
- binär-dezimal und andersum

@ -0,0 +1,64 @@
// Sample EDC
images {
image, "logo.png" LOSSY 95;
}
collections {
group {
name, "Unit Converter";
min: 480 640;
parts {
part {
name, "background";
type, IMAGE;
mouse_events, 0;
description {
state, "default" 0.0;
rel1 {
relative, 0.0 0.0;
offset, 0 0;
}
rel2 {
relative, 1.0 1.0;
offset, -1 -1;
}
image {
normal, "logo.png";
}
}
}
part {
name, "fromfield";
type, TEXTBLOCK;
mouse_events, 1;
description {
state, "default" 0.0;
min, 100 50;
max, 100 50;
align, 0.5 0.5;
color, 211 168 234 255;
rel1 {
relative, 0.0 0.0;
offset, 0 0;
}
rel2 {
relative, 1.0 1.0;
offset, -1 -1;
}
}
}
} /* Close Parts */
} /* Close Group */
} /* Close Coll */

@ -0,0 +1,62 @@
// Sample EDC
images {
image, "background.png" LOSSY 95;
}
collections {
group {
name, "Unit Converter";
min: 480 640;
parts {
part {
name, "background";
type, IMAGE;
mouse_events, 0;
description {
state, "default" 0.0;
rel1 {
relative, 0.0 0.0;
offset, 0 0;
}
rel2 {
relative, 1.0 1.0;
offset, -1 -1;
}
image {
normal, "background.png";
}
}
}
part {
name, "fromfield";
type, TEXTBLOCK;
mouse_events, 1;
description {
state, "default" 0.0;
min, 100 50;
max, 100 50;
align, 0.5 0.5;
rel1 {
relative, 0.0 0.0;
offset, 0 0;
}
rel2 {
relative, 1.0 1.0;
offset, -1 -1;
}
}
}
} /* Close Parts */
} /* Close Group */
} /* Close Coll */

Binary file not shown.

@ -0,0 +1,111 @@
<?xml version="1.0" ?>
<convertfactors version='2008-02-13'>
<unitgroup>
<category
name="length"
refunit="m"
>
<unit
name="mm"
from_ref ="result / 0.001"
to_ref ="number * 0.001"
/>
<unit
name="cm"
from_ref="result / 0.01"
to_ref="number * 0.01"
/>
<unit
name="dm"
from_ref="result / 0.1"
to_ref="number * 0.1"
/>
<unit
name="m"
from_ref="result"
to_ref="number"
/>
<unit
name="km"
from_ref="result / 1000"
to_ref="number * 1000"
/>
</category>
<category
name="currency"
refunit="euro"
>
<unit
name="Euro"
from_ref="result"
to_ref="number"
/>
<unit
name="USD"
from_ref="result / 1.4573"
to_ref="number * 1.4573"
/>
<unit
name="TWD"
from_ref="result * 46.125"
to_ref="number / 46.125"
/>
<unit
name="DM"
from_ref="result * 1.95583"
to_ref="number / 1.95583"
/>
</category>
<category
name="temperature"
refunit="celsius"
>
<unit
name="celsius"
from_ref="result"
to_ref="number"
/>
<unit
name="kelvin"
from_ref="273.15 - result"
to_ref="number - 273.15"
/>
<unit
name="fahrenheit"
to_ref="(number - 32) / 1.8"
from_ref="result * 1.8 + 32"
/>
</category>
<category
name="wind speed"
refunit="m/s"
>
<unit
name="m/s"
from_ref="result"
to_ref="number"
/>
<unit
name="km/h"
from_ref="result/1000*3600"
to_ref="number*1000/3600"
/>
<unit
name="sm/h"
from_ref="result / 0.514"
to_ref="number * 0.514"
/>
<unit
name="mph"
from_ref="result / 0.447"
to_ref="number * 0.447"
/>
<unit
name="bft"
from_ref="(9 + 6 * result) ** 0.5 -3"
to_ref="number + number * number / 6"
/>
</category>
</unitgroup>
</convertfactors>

@ -0,0 +1,28 @@
<?xml version="1.0" ?>
<convertfactors>
<version>2007-01-17</version>
<unitgroup>
<category>length</category>
<refunit>m</refunit>
<formular>number * unit1_factor / unit2_factor</formular>
<unit>mm</unit>
<factor>0,001</factor>
<unit>cm</unit>
<factor>0,01</factor>
<unit>dm</unit>
<factor>0,1</factor>
<unit>m</unit>
<factor>1</factor>
<unit>km</unit>
<factor>1000</factor>
</unitgroup>
<unitgroup>
<category>currency</category>
<refunit>euro</refunit>
<unit>euro</unit>
<factor>1</factor>
<unit>DM</unit>
<factor>1,95583</factor>
</unitgroup>
</convertfactors>

@ -0,0 +1,174 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# unit_converter - Converts between different units
import pygtk
pygtk.require('2.0')
import gtk
from lxml import etree
from StringIO import StringIO
unitfile = 'unit.xml'
file = etree.parse(unitfile) #parse the xml file
class unitconvert(object):
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def on_catcombo_changed(self, widget):
self.create_menu()
def categorymenu(self): # creates the data for the category menu from the xml file
category = []
for categoryname in file.findall('//category'): # browses through the elements in the file
category.append(categoryname.get('name')) # append the name to the category list
return category
def unitmenu(self): # creates the data for the unit menu
unit = []
category_set = self.catcombo.get_active_text() # checks for the active category to add this units
for categorynames in file.findall('//category'): # search the categorys
if categorynames.get('name') == category_set: # search through the file according to the set category
for units in categorynames.getchildren(): # get the units from the specific category
unit.append(units.get('name')) # appends the units from the category into the unit list
return unit
def create_menu(self): # fill the gui with the data from categorymenu and unitmenu
if self.catcombo.get_active_text() == None: # if no category menu exists
for categorys in self.categorymenu():
self.catcombo.append_text(categorys) # append the categorys to the menu
modelfrom = self.fromcombo.get_model() # get the data from the fromcombo field ...
modelfrom.clear() # ... and delete them for the new data
modelto = self.tocombo.get_model() # the same as fromcombo
modelto.clear()
for units in self.unitmenu():
self.fromcombo.append_text(units) # filling the unit menus
self.tocombo.append_text(units)
def convert(self, widget):
category_set = self.catcombo.get_active_text()
unit2 = self.tocombo.get_active_text()
unit1 = self.fromcombo.get_active_text()
number = float(self.fromentry.get_text().replace(',','.')) # save the number as a float object and replace the commas with dots for the internal calculation
fromref = ''
toref = ''
for categorynames in file.findall('//category'): # search the categorys
if categorynames.get('name') == category_set: # search through the file according to the set category
for units in categorynames.getchildren(): # get the units from the specific category
if units.get('name') == unit1: # the name from the unit in the category musst set in the to field
toref = units.get('to_ref') # search in this unit for the to_ref formular
if units.get('name') == unit2: # the same as above only with the from field
fromref = units.get('from_ref')
result = eval(toref) # execute the formular in the toref field and safe it to result
endresult = eval(fromref) # the same as above
printresult = str(endresult).replace('.',',') # for the better readability replace the dot with a comma
self.toentry.set_text(printresult) # sets the result into the to field
def clear(self, widget): # clears the entry fields
self.fromentry.set_text('')
self.toentry.set_text('')
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_size_request(480,640)
self.window.set_title('Unit Converter')
self.window.connect('delete_event', self.delete_event)
self.window.connect('destroy', self.destroy)
# create a box in the dimensions of the window and add it to them
self.box = gtk.VBox(False,2)
self.window.add(self.box)
# create a Hbox for the category and add them to self.box
self.catbox = gtk.HBox(True,2)
# frame for the category box
self.catframe = gtk.Frame('Category')
self.box.pack_start(self.catframe, False, False, 0)
# a combobox for the categorys
self.catcombo = gtk.combo_box_new_text()
self.catcombo.connect('changed', self.on_catcombo_changed)
self.catbox.pack_start(self.catcombo, False, False, 0)
self.catframe.add(self.catbox)
#create a vbox for the From units
self.frombox = gtk.VBox(False,2)
#frame for the from box
self.fromframe = gtk.Frame('Convert from ...')
self.box.pack_start(self.fromframe, False, False, 0)
self.fromframe.add(self.frombox)
#create a entry for the 'Convert from' field
self.fromentry = gtk.Entry()
self.fromentry.set_text('Convert from ...')
self.frombox.pack_start(self.fromentry, False, False, 0)
#a combobox for the from unit
self.fromcombo = gtk.combo_box_new_text()
self.frombox.pack_start(self.fromcombo, False, False, 0)
#create a vbox for the To units
self.tobox = gtk.VBox(False,2)
#frame for the to box
self.toframe = gtk.Frame('Convert to ...')
self.box.pack_start(self.toframe, False, False, 0)
self.toframe.add(self.tobox)
#create a entry for the 'Convert to' field
self.toentry = gtk.Entry()
self.toentry.set_text('Convert to ...')
self.tobox.pack_start(self.toentry, False, False, 0)
#a combobox for the to unit
self.tocombo = gtk.combo_box_new_text()
self.tobox.pack_start(self.tocombo, False, False , 0)
#create a box for the go and clear button
self.sendbox = gtk.HBox(True,2)
self.box.pack_end(self.sendbox, False, False, 0)
self.gobutton = gtk.Button('Go')
self.sendbox.pack_start(self.gobutton, False, False, 0)
self.gobutton.connect('clicked', self.convert)
self.clearbutton = gtk.Button('Clear')
self.sendbox.pack_start(self.clearbutton, False, False, 0)
self.clearbutton.connect('clicked', self.clear)
# Display all elements
self.box.show()
self.catbox.show()
self.catcombo.show()
self.catframe.show()
self.fromentry.show()
self.fromcombo.show()
self.fromframe.show()
self.frombox.show()
self.toentry.show()
self.tocombo.show()
self.toframe.show()
self.tobox.show()
self.sendbox.show()
self.clearbutton.show()
self.gobutton.show()
self.window.show()
def main():
gtk.main()
return 0
if __name__ == '__main__':
unitconvert().create_menu()
main()

@ -0,0 +1,310 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# unit_converter - Converts between different units
# version 0.1
# 2008/02/13
# by Patrick Beck
import pygtk
pygtk.require('2.0')
import gtk
import xml.dom.minidom
from sys import exit
try:
unitfile = 'unit.xml' # path to the unit file
file = xml.dom.minidom.parse(unitfile) # parse the xml file
except:
print 'The unitfile can not be opened or found'
exit()
class unitconvert(object):
toactive = 0 # status variable will be set to 1, when the 'to' field is the last activated field
fromactive = 0 # the same, only for the 'from' field
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def fromentry_changed(self, widget):
self.fromactive = 1
self.toactive = 0
def toentry_changed(self, widget):
self.toactive = 1
self.fromactive = 0
def on_catcombo_changed(self, widget):
self.create_menu()
def category_isset(self): # get the selected category
categoryname = self.catcombo.get_active_text()
return categoryname
def unit_from_isset(self): # get the selected unit from the 'from' field
unit_from = self.fromcombo.get_active_text()
return unit_from
def unit_to_isset(self): # get the selected unit from the 'to' field
unit_to = self.tocombo.get_active_text()
return unit_to
def number_from_isset(self): # get the data from the 'from' entry field
getnumber_from = self.fromentry.get_text()
try: # Test if the entry is a number
number_from = float(getnumber_from.replace(',','.')) # replace the comma with a dot for the internal calculation
return number_from
except:
return 'nonnumber'
def number_to_isset(self): # get the data for the 'to' entry field
getnumber_to = self.toentry.get_text()
try: # Test if the entry is a number
number_to = float(getnumber_to.replace(',','.'))
return number_to
except:
return 'nonnumber'
def fromentry_set(self, printresult): # sets the result in the target field
self.fromentry.set_text(printresult)
def toentry_set(self, printresult): # the same as above
self.toentry.set_text(printresult)
def categorymenu(self): # creates the data for the category menu from the xml file
category = []
for categoryname in file.getElementsByTagName('category'): # browses through the elements in the file
category.append(categoryname.getAttribute('name')) # append the name to the category list
return category
def unitmenu(self): # creates the data for the unit menu
unit = []
set_category = self.category_isset() # checks for the active category to add this units
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == set_category: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
unit.append(units.getAttribute('name')) # appends the units from the category into the unit list
return unit
def create_menu(self): # fill the gui with the data from categorymenu and unitmenu
if self.category_isset() == None: # if no category menu exists
for categorys in self.categorymenu():
self.catcombo.append_text(categorys) # append the categorys to the menu
modelfrom = self.fromcombo.get_model() # get the data from the fromcombo field ...
modelfrom.clear() # ... and delete them for the new data
modelto = self.tocombo.get_model() # the same as fromcombo
modelto.clear()
for units in self.unitmenu():
self.fromcombo.append_text(units) # filling the unit menus
self.tocombo.append_text(units)
def convert(self, widget):
category_isset = self.category_isset() # get the active category from the gui
unit_to = self.unit_to_isset() # gets the active unit from the 'to' field
unit_from = self.unit_from_isset() # gets the same for the 'from' field
# a few tests for the inputs in the combo and entry field
if category_isset == None:
self.fromentry_set('Choose a category')
self.toentry_set('and the units')
elif unit_from == None and unit_to == None:
self.fromentry_set('Choose a unit')
self.toentry_set('Choose a unit')
elif unit_from == None:
self.fromentry_set('Choose a unit')
self.toentry_set('')
elif unit_to == None:
self.toentry_set('Choose a unit')
self.fromentry_set('')
# when the inputs are ok excecute the rest of the program
else:
if self.toactive == 1:
number = self.number_to_isset()
if number == 'nonnumber': # when the entry is not a number print a error
self.toentry_set('Error - put in a number')
else:
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == category_isset: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
if units.getAttribute('name') == unit_from: # the name from the unit in the category musst set in the to field
if self.fromactive == 1: # when active gets the to_ref field
toref = units.getAttribute('to_ref')
else:
fromref = units.getAttribute('from_ref') # else the from_ref field
if units.getAttribute('name') == unit_to:
if self.fromactive == 1:
fromref = units.getAttribute('from_ref')
else:
toref = units.getAttribute('to_ref')
# if self.fromactive == 1: # select the basis for the calculation => when the from field was changed use the data from this field
# number = number_from
# if self.toactive == 1:
# number = number_to
result = eval(toref) # execute the formular in the toref field and safe it to result
endresult = eval(fromref) # convert from the refunit to the target unit
printresult = str(endresult).replace('.',',') # for the better readability replace the dot with a comma
# if self.fromactive == 1:
# self.toentry_set(printresult) # sets the result into the 'from' field
# else:
self.fromentry_set(printresult) # same as above for the 'to' field
else:
number = self.number_from_isset()
if number == 'nonnumber': # when the entry is not a number print a error
self.fromentry_set('Error - put in a number')
else:
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == category_isset: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
if units.getAttribute('name') == unit_from: # the name from the unit in the category musst set in the to field
if self.fromactive == 1: # when active gets the to_ref field
toref = units.getAttribute('to_ref')
else:
fromref = units.getAttribute('from_ref') # else the from_ref field
if units.getAttribute('name') == unit_to:
if self.fromactive == 1:
fromref = units.getAttribute('from_ref')
else:
toref = units.getAttribute('to_ref')
# if self.fromactive == 1: # select the basis for the calculation => when the from field was changed use the data from this field
# number = number_from
# if self.toactive == 1:
# number = number_to
result = eval(toref) # execute the formular in the toref field and safe it to result
endresult = eval(fromref) # convert from the refunit to the target unit
printresult = str(endresult).replace('.',',') # for the better readability replace the dot with a comma
# if self.fromactive == 1:
self.toentry_set(printresult) # sets the result into the 'from' field
# else:
# self.fromentry_set(printresult) # same as above for the 'to' field
def clear(self, widget): # clears the entry fields
self.fromentry.set_text('')
self.toentry.set_text('')
def fromentry_cursor(self, widget, data=None):
self.fromentry.set_text('')
def toentry_cursor(self, widget, data=None):
self.toentry.set_text('')
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title('Unit Converter')
self.window.connect('delete_event', self.delete_event)
self.window.connect('destroy', self.destroy)
# create a box in the dimensions of the window and add it to them
self.box = gtk.VBox(False,2)
self.window.add(self.box)
# create a Hbox for the category and add them to self.box
self.catbox = gtk.HBox(True,2)
# frame for the category box
self.catframe = gtk.Frame('Category')
self.box.pack_start(self.catframe, False, False, 0)
# a combobox for the categorys
self.catcombo = gtk.combo_box_new_text()
self.catcombo.connect('changed', self.on_catcombo_changed)
self.catbox.pack_start(self.catcombo, False, False, 0)
self.catframe.add(self.catbox)
#create a vbox for the From units
self.frombox = gtk.VBox(False,2)
#frame for the from box
self.fromframe = gtk.Frame('Convert from ...')
self.box.pack_start(self.fromframe, False, False, 0)
self.fromframe.add(self.frombox)
#create a entry for the 'Convert from' field
self.fromentry = gtk.Entry()
self.fromentry.set_text('Input a number here ')
self.fromentry.connect('changed', self.fromentry_changed)
self.fromentry.connect('activate', self.convert)
self.fromentry.connect('focus_in_event', self.fromentry_cursor)
self.frombox.pack_start(self.fromentry, False, False, 0)
#a combobox for the from unit
self.fromcombo = gtk.combo_box_new_text()
self.frombox.pack_start(self.fromcombo, False, False, 0)
#create a vbox for the To units
self.tobox = gtk.VBox(False,2)
#frame for the to box
self.toframe = gtk.Frame('Convert to ...')
self.box.pack_start(self.toframe, False, False, 0)
self.toframe.add(self.tobox)
#create a entry for the 'Convert to' field
self.toentry = gtk.Entry()
self.toentry.set_text('... or here')
self.toentry.connect('changed', self.toentry_changed)
self.toentry.connect('activate', self.convert)
self.toentry.connect('focus_in_event', self.toentry_cursor)
self.tobox.pack_start(self.toentry, False, False, 0)
#a combobox for the to unit
self.tocombo = gtk.combo_box_new_text()
self.tobox.pack_start(self.tocombo, False, False , 0)
#create a box for the go and clear button
self.sendbox = gtk.HBox(True,2)
self.box.pack_end(self.sendbox, False, False, 0)
self.gobutton = gtk.Button('Go')
self.sendbox.pack_start(self.gobutton, True, True, 0)
self.gobutton.connect('clicked', self.convert)
self.clearbutton = gtk.Button('Clear')
self.sendbox.pack_start(self.clearbutton, True, True, 0)
self.clearbutton.connect('clicked', self.clear)
# Display all elements
self.box.show()
self.catbox.show()
self.catcombo.show()
self.catframe.show()
self.fromentry.show()
self.fromcombo.show()
self.fromframe.show()
self.frombox.show()
self.toentry.show()
self.tocombo.show()
self.toframe.show()
self.tobox.show()
self.sendbox.show()
self.clearbutton.show()
self.gobutton.show()
self.window.show()
def main():
gtk.main()
return 0
if __name__ == '__main__':
unitconvert().create_menu()
main()

@ -0,0 +1,310 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# unit_converter - Converts between different units
# version 0.1
# 2008/02/13
# by Patrick Beck
import pygtk
pygtk.require('2.0')
import gtk
import xml.dom.minidom
from sys import exit
try:
unitfile = 'unit.xml' # path to the unit file
file = xml.dom.minidom.parse(unitfile) # parse the xml file
except:
print 'The unitfile can not be opened or found'
exit()
class unitconvert(object):
toactive = 0 # status variable will be set to 1, when the 'to' field is the last activated field
fromactive = 0 # the same, only for the 'from' field
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
gtk.main_quit()
def fromentry_changed(self, widget):
self.fromactive = 1
self.toactive = 0
def toentry_changed(self, widget):
self.toactive = 1
self.fromactive = 0
def on_catcombo_changed(self, widget):
self.create_menu()
def category_isset(self): # get the selected category
categoryname = self.catcombo.get_active_text()
return categoryname
def unit_from_isset(self): # get the selected unit from the 'from' field
unit_from = self.fromcombo.get_active_text()
return unit_from
def unit_to_isset(self): # get the selected unit from the 'to' field
unit_to = self.tocombo.get_active_text()
return unit_to
def number_from_isset(self): # get the data from the 'from' entry field
getnumber_from = self.fromentry.get_text()
try: # Test if the entry is a number
number_from = float(getnumber_from.replace(',','.')) # replace the comma with a dot for the internal calculation
return number_from
except:
return 'nonnumber'
def number_to_isset(self): # get the data for the 'to' entry field
getnumber_to = self.toentry.get_text()
try: # Test if the entry is a number
number_to = float(getnumber_to.replace(',','.'))
return number_to
except:
return 'nonnumber'
def fromentry_set(self, printresult): # sets the result in the target field
self.fromentry.set_text(printresult)
def toentry_set(self, printresult): # the same as above
self.toentry.set_text(printresult)
def categorymenu(self): # creates the data for the category menu from the xml file
category = []
for categoryname in file.getElementsByTagName('category'): # browses through the elements in the file
category.append(categoryname.getAttribute('name')) # append the name to the category list
return category
def unitmenu(self): # creates the data for the unit menu
unit = []
set_category = self.category_isset() # checks for the active category to add this units
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == set_category: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
unit.append(units.getAttribute('name')) # appends the units from the category into the unit list
return unit
def create_menu(self): # fill the gui with the data from categorymenu and unitmenu
if self.category_isset() == None: # if no category menu exists
for categorys in self.categorymenu():
self.catcombo.append_text(categorys) # append the categorys to the menu
modelfrom = self.fromcombo.get_model() # get the data from the fromcombo field ...
modelfrom.clear() # ... and delete them for the new data
modelto = self.tocombo.get_model() # the same as fromcombo
modelto.clear()
for units in self.unitmenu():
self.fromcombo.append_text(units) # filling the unit menus
self.tocombo.append_text(units)
def convert(self, widget):
category_isset = self.category_isset() # get the active category from the gui
unit_to = self.unit_to_isset() # gets the active unit from the 'to' field
unit_from = self.unit_from_isset() # gets the same for the 'from' field
# a few tests for the inputs in the combo and entry field
if category_isset == None:
self.fromentry_set('Choose a category')
self.toentry_set('and the units')
elif unit_from == None and unit_to == None:
self.fromentry_set('Choose a unit')
self.toentry_set('Choose a unit')
elif unit_from == None:
self.fromentry_set('Choose a unit')
self.toentry_set('')
elif unit_to == None:
self.toentry_set('Choose a unit')
self.fromentry_set('')
# when the inputs are ok excecute the rest of the program
else:
if self.toactive == 1:
number = self.number_to_isset()
if number == 'nonnumber': # when the entry is not a number print a error
self.toentry_set('Error - put in a number')
else:
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == category_isset: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
if units.getAttribute('name') == unit_from: # the name from the unit in the category musst set in the to field
if self.fromactive == 1: # when active gets the to_ref field
toref = units.getAttribute('to_ref')
else:
fromref = units.getAttribute('from_ref') # else the from_ref field
if units.getAttribute('name') == unit_to:
if self.fromactive == 1:
fromref = units.getAttribute('from_ref')
else:
toref = units.getAttribute('to_ref')
# if self.fromactive == 1: # select the basis for the calculation => when the from field was changed use the data from this field
# number = number_from
# if self.toactive == 1:
# number = number_to
result = eval(toref) # execute the formular in the toref field and safe it to result
endresult = eval(fromref) # convert from the refunit to the target unit
printresult = str(endresult).replace('.',',') # for the better readability replace the dot with a comma
# if self.fromactive == 1:
# self.toentry_set(printresult) # sets the result into the 'from' field
# else:
self.fromentry_set(printresult) # same as above for the 'to' field
else:
number = self.number_from_isset()
if number == 'nonnumber': # when the entry is not a number print a error
self.fromentry_set('Error - put in a number')
else:
for categorynames in file.getElementsByTagName('category'): # search the categorys
if categorynames.getAttribute('name') == category_isset: # search through the file according to the set category
for units in categorynames.getElementsByTagName('unit'): # get the units from the specific category
if units.getAttribute('name') == unit_from: # the name from the unit in the category musst set in the to field
if self.fromactive == 1: # when active gets the to_ref field
toref = units.getAttribute('to_ref')
else:
fromref = units.getAttribute('from_ref') # else the from_ref field
if units.getAttribute('name') == unit_to:
if self.fromactive == 1:
fromref = units.getAttribute('from_ref')
else:
toref = units.getAttribute('to_ref')
# if self.fromactive == 1: # select the basis for the calculation => when the from field was changed use the data from this field
# number = number_from
# if self.toactive == 1:
# number = number_to
result = eval(toref) # execute the formular in the toref field and safe it to result
endresult = eval(fromref) # convert from the refunit to the target unit
printresult = str(endresult).replace('.',',') # for the better readability replace the dot with a comma
# if self.fromactive == 1:
self.toentry_set(printresult) # sets the result into the 'from' field
# else:
# self.fromentry_set(printresult) # same as above for the 'to' field
def clear(self, widget): # clears the entry fields
self.fromentry.set_text('')
self.toentry.set_text('')
def fromentry_cursor(self, widget, data=None):
self.fromentry.set_text('')
def toentry_cursor(self, widget, data=None):
self.toentry.set_text('')
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title('Unit Converter')
self.window.connect('delete_event', self.delete_event)
self.window.connect('destroy', self.destroy)
# create a box in the dimensions of the window and add it to them
self.box = gtk.VBox(False,2)
self.window.add(self.box)
# create a Hbox for the category and add them to self.box
self.catbox = gtk.HBox(True,2)
# frame for the category box
self.catframe = gtk.Frame('Category')
self.box.pack_start(self.catframe, False, False, 0)
# a combobox for the categorys
self.catcombo = gtk.combo_box_new_text()
self.catcombo.connect('changed', self.on_catcombo_changed)
self.catbox.pack_start(self.catcombo, False, False, 0)
self.catframe.add(self.catbox)
#create a vbox for the From units
self.frombox = gtk.VBox(False,2)
#frame for the from box
self.fromframe = gtk.Frame('Convert from ...')
self.box.pack_start(self.fromframe, False, False, 0)
self.fromframe.add(self.frombox)
#create a entry for the 'Convert from' field
self.fromentry = gtk.Entry()
self.fromentry.set_text('Input a number here ')
self.fromentry.connect('changed', self.fromentry_changed)
self.fromentry.connect('activate', self.convert)
self.fromentry.connect('focus_in_event', self.fromentry_cursor)
self.frombox.pack_start(self.fromentry, False, False, 0)
#a combobox for the from unit
self.fromcombo = gtk.combo_box_new_text()
self.frombox.pack_start(self.fromcombo, False, False, 0)
#create a vbox for the To units
self.tobox = gtk.VBox(False,2)
#frame for the to box
self.toframe = gtk.Frame('Convert to ...')
self.box.pack_start(self.toframe, False, False, 0)
self.toframe.add(self.tobox)
#create a entry for the 'Convert to' field
self.toentry = gtk.Entry()
self.toentry.set_text('... or here')
self.toentry.connect('changed', self.toentry_changed)
self.toentry.connect('activate', self.convert)
self.toentry.connect('focus_in_event', self.toentry_cursor)
self.tobox.pack_start(self.toentry, False, False, 0)
#a combobox for the to unit
self.tocombo = gtk.combo_box_new_text()
self.tobox.pack_start(self.tocombo, False, False , 0)
#create a box for the go and clear button
self.sendbox = gtk.HBox(True,2)
self.box.pack_end(self.sendbox, False, False, 0)
self.gobutton = gtk.Button('Go')
self.sendbox.pack_start(self.gobutton, True, True, 0)
self.gobutton.connect('clicked', self.convert)
self.clearbutton = gtk.Button('Clear')
self.sendbox.pack_start(self.clearbutton, True, True, 0)
self.clearbutton.connect('clicked', self.clear)
# Display all elements
self.box.show()
self.catbox.show()
self.catcombo.show()
self.catframe.show()
self.fromentry.show()
self.fromcombo.show()
self.fromframe.show()
self.frombox.show()
self.toentry.show()
self.tocombo.show()
self.toframe.show()
self.tobox.show()
self.sendbox.show()
self.clearbutton.show()
self.gobutton.show()
self.window.show()
def main():
gtk.main()
return 0
if __name__ == '__main__':
unitconvert().create_menu()
main()

@ -0,0 +1,12 @@
#!/usr/bin/env python
import xml.dom.minidom
unitfile = 'unit.xml'
file = xml.dom.minidom.parse(unitfile)
name = file.documentElement
for categorynames in file.getElementsByTagName('category'):
if categorynames.getAttribute('name') == 'length':

@ -0,0 +1,43 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lxml import etree
from StringIO import StringIO
def xmlpaste(tag):
file = etree.parse('unit.xml')
categoryname = 'currency'
for category in file.findall('//category'):
if category.get('name') == categoryname:
for units in category.getchildren():
print units.get('name')
# for i in file.getiterator('category'):
# for f in i.getchildren():
# print f.get('name')
# for category in file.findall('//category'):
# category.get('name')
# for i in file.findall('//unit'):
# i.get('name')
# print file.findtext('currency')
# print i.tag, i.text
# for child in i.getchildren():
# print i.text
# print child.get('name')
# units = []
# for unit in file.getiterator('unit'):
# units.append(unit.text)
# factors = []
# for factor in file.getiterator('factor'):
# factors.append(factor.text)
# i = 0
# content = len(units)
#
# while i != content:
# print units[i],
# print factors[i]
# i += 1
#
xmlpaste('unit')
Loading…
Cancel
Save