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"

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()