meta data for this page
  •  

This is an old revision of the document!


Shared Python Code

This is our own shared code page. Here you can/should add ready-made, good and working code snippets for other to use. It is highly recommended to share stuff, AS IT MAKES EVERYONE'S LIFE EASIER. No need to invent the wheel twice.

So, add the codes underneath. You can copy the template underneath and just add to the line:

Template

  • Group: Group name
  • Description: What this snippet does
example.py
# This is just a print hello world! example
print "Hello World"

Making Phone Calls

  • Group: Japskua
  • Description: Makes a phone call to given number
call.py
'''
Created on Aug 18, 2011
 
@author: Japskua
'''
 
import dbus
 
class Caller(object):
    """ 
    @summary:  This is the Caller class that makes the phone calls
    """
 
    def __init__(self):
        """ 
        @summary: The Constructor 
        """
        # Do nothing
        self.bus = dbus.SystemBus()
        self.csd_call = dbus.Interface(self.bus.get_object("com.nokia.csd",
                                                      "/com/nokia/csd/call"),
                                                      "com.nokia.csd.Call")
 
    def Call(self, number):
        """ 
        @summary: The function to Call to a given number 
        @param number: The number to call to
        @type number: String
        """
        self.csd_call.CreateWith(str(number), dbus.UInt32(0))
 
 
if __name__ == '__main__':
    caller = Caller()
    caller.Call("0400558160")

Creating Buttons with Images

  • Group: Japskua
  • Description: How to create buttons with functions, data and images
  • * Creating multiple buttons
  • * Adding functions to buttons
  • * Adding images to buttons

The image used here: The "Home" Button

window_with_buttons.py
# First import all the neccesary libraries
import gtk
import hildon
 
# This is an example callback function that can be put to a button and used to receive stuff
# All buttons print hello world, but if it has data included, will print that aswell
def hello(widget, data):
    print "Hello World!"
    if data != None:
        print "Data: ", data
 
def main():
    # Get an instance of HildonProgram. It is an object used to represent an
    # application running in the Hildon framework.
    program = hildon.Program.get_instance()
 
    # create a new hildon window
    window = hildon.Window()
 
    # Registers a window as belonging to the program
    program.add_window(window)
 
    # When the window is given the "delete_event" signal (this is given by the
    # window manager, usually by the "close" option, or on the titlebar), we
    # ask it to call the delete_event () function as defined above. The data
    # passed to the callback function is None and is ignored in the callback
    # function.
    window.connect("delete_event", gtk.main_quit, None)
 
    # Create three different buttons: 1st with no data, 2nd with data and 3rd as an Image
    button1 = hildon.Button(gtk.HILDON_SIZE_AUTO, hildon.BUTTON_ARRANGEMENT_VERTICAL, "First Button, no data")
    button2 = hildon.Button(gtk.HILDON_SIZE_AUTO, hildon.BUTTON_ARRANGEMENT_VERTICAL, "Second Button, with data")
    button3 = hildon.Button(gtk.HILDON_SIZE_AUTO, hildon.BUTTON_ARRANGEMENT_VERTICAL, "")
 
    # When the button is given the "clicked" signal, we ask it to call the
    # hello () function as defined above. If the data passed to the callback
    # function is None and is ignored in the callback function.
    button1.connect("clicked", hello, None)
    button2.connect("clicked", hello, "a lot of numbers here")
    button3.connect("clicked", hello, None)
 
    # Set the button to have an image
    # First we need to load the image from file
    image = gtk.image_new_from_file("preview.jpg")
    # and then to set it to the button
    button3.set_image(image)
 
    # Now we create a button box, which is an container that can have multiple buttons in it
    # The one used here is Vertical box, that places the buttons on top of each other, but
    # you could create HButtonBox() that is a horizontal box, that puts buttons next to
    # each other
    buttonBox = gtk.VButtonBox()
 
    # Here the layour is defined to spread the buttons evenly, options to use here are
    # gtk.BUTTONBOX_SPREAD, gtk.BUTTONBOX_EDGE, gtk.BUTTONBOX_START and gtk.BUTTONBOX_END
    buttonBox.set_layout(gtk.BUTTONBOX_SPREAD)
 
    # And then finally add all the buttons to the buttonBox
    buttonBox.add(button1)
    buttonBox.add(button2)
    buttonBox.add(button3)
    # And then the buttonBox to the window
    window.add(buttonBox)
 
 
 
    # The final step is to display this newly created widget and all widgets it
    # contains.
    window.show_all()
 
    # All GTK+ applications must have a gtk_main(). Control ends here and waits
    # for an event to occur (like a key press or mouse event).
    gtk.main()
 
if __name__ == "__main__":
    main()

Creating buttons

  • Group: Group 2
  • Description: It creates 2 buttons and 2 labels, pressing the buttons change the text of one of the labels.
buttons.py
'''
Created on Aug 18, 2011
 
@author: Group 2
@summary: Button example
'''
 
import sys
import gtk
import hildon
 
def menu_button_clicked(button, label):
    buttontext = button.get_label()
    text = "Last option selected:\n%s" % buttontext
    label.set_text(text)
 
def main():
    win = hildon.StackableWindow()
 
    # Create and pack labels
    vbox = gtk.VBox(False, 10)
    hbox = gtk.HBox(False, 10)
 
    #interface.. buttons, labels ,... etc
    labelHeader = gtk.Label("Application header")
    labelSubHeader = gtk.Label("-")
 
    buttonGTK1 = gtk.Button("None")
    buttonGTK1.connect("clicked", menu_button_clicked, labelSubHeader)
 
    buttonGTK2 = gtk.Button("Hello")
    buttonGTK2.connect("clicked", menu_button_clicked, labelSubHeader)
 
    hbox.pack_start(buttonGTK1, True, True, 0)
    hbox.pack_start(buttonGTK2, True, True, 0)
 
    vbox.pack_start(labelHeader, True, True, 0)
    vbox.pack_start(labelSubHeader, True, True, 0)
    vbox.pack_start(hbox, True, True, 0)
 
    # Add label's box to window
    win.add(vbox)
 
    win.connect("delete_event", gtk.main_quit)
 
    win.show_all()
 
    gtk.main()
 
if __name__ == "__main__":
    main()