Editing PyMaemo/UI tutorial/Data selection

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 114: Line 114:
[[Image:example-single-column-selector.png|400px]]
[[Image:example-single-column-selector.png|400px]]
<source lang="python">
<source lang="python">
-
# Based on C code from:
+
    # Based on C code from:
-
# "Hildon Tutorial" version 2009-04-28
+
    # "Hildon Tutorial" version 2009-04-28
-
# Example 6.2 "Example of a selector with a custom column"
+
    # Example 6.2 "Example of a selector with a custom column"
-
 
+
   
-
import gtk
+
    import gtk
-
import hildon
+
    import hildon
-
import gobject
+
    import gobject
-
 
+
   
-
def selection_changed(selector, user_data):
+
    def selection_changed(selector, user_data):
-
    current_selection = selector.get_current_text()
+
        current_selection = selector.get_current_text()
-
    print "Current selection : %s" % (current_selection)
+
        print "Current selection : %s" % (current_selection)
-
 
+
   
-
def create_customized_selector():
+
    def create_customized_selector():
-
    # Create a touch selector  
+
        # Create a touch selector  
-
    selector = hildon.TouchSelector()
+
        selector = hildon.TouchSelector()
-
 
+
      
-
     # Stock icons will be used for the example
+
        # Stock icons will be used for the example
-
    icon_list = gtk.stock_list_ids()
+
        icon_list = gtk.stock_list_ids()
-
 
+
      
-
     # Create model to store selector's items  
+
        # Create model to store selector's items  
-
    store_icons = gtk.ListStore(gobject.TYPE_STRING)
+
        store_icons = gtk.ListStore(gobject.TYPE_STRING)
-
 
+
      
-
     # Populate model
+
        # Populate model
-
    for item in icon_list:
+
        for item in icon_list:
-
        new_iter = store_icons.append()
+
            new_iter = store_icons.append()
-
        store_icons.set(new_iter, 0, item)
+
            store_icons.set(new_iter, 0, item)
-
 
+
      
-
     # Create and set up a pixbuf renderer to use in the selector  
+
        # Create and set up a pixbuf renderer to use in the selector  
-
    renderer = gtk.CellRendererPixbuf()  
+
        renderer = gtk.CellRendererPixbuf()  
-
    renderer.set_fixed_size(-1, 100)
+
        renderer.set_fixed_size(-1, 100)
-
 
+
   
-
 
+
      
-
     # Add the column to the selector
+
        # Add the column to the selector
-
    column = selector.append_column(store_icons, renderer, stock_id = 0)
+
        column = selector.append_column(store_icons, renderer, stock_id = 0)
-
 
+
      
-
     # Set the selection mode
+
        # Set the selection mode
-
    selector.set_column_selection_mode(hildon.TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)
+
        selector.set_column_selection_mode(hildon.TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)
-
 
+
      
-
     # Set the property "text-column" that indicates the column
+
        # Set the property "text-column" that indicates the column
-
    # of the model to get the string from
+
        # of the model to get the string from
-
    column.set_property("text-column", 0)
+
        column.set_property("text-column", 0)
-
 
+
      
-
     return selector
+
        return selector
-
 
+
   
-
def app_quit(widget, data=None):
+
    def app_quit(widget, data=None):
-
    gtk.main_quit()
+
        gtk.main_quit()
-
 
+
   
-
def main():
+
    def main():
-
    program = hildon.Program.get_instance()
+
        program = hildon.Program.get_instance()
-
    gtk.set_application_name("hildon-touch-selector example program")
+
        gtk.set_application_name("hildon-touch-selector example program")
-
 
+
      
-
     window = hildon.StackableWindow()
+
        window = hildon.StackableWindow()
-
    program.add_window(window)
+
        program.add_window(window)
-
 
+
      
-
     # Create touch selector
+
        # Create touch selector
-
    selector = create_customized_selector()
+
        selector = create_customized_selector()
-
    window.add(selector)
+
        window.add(selector)
-
 
+
      
-
     window.connect("destroy", app_quit)
+
        window.connect("destroy", app_quit)
-
 
+
      
-
     window.show_all()
+
        window.show_all()
-
 
+
      
-
     gtk.main()
+
        gtk.main()
-
 
+
   
-
if __name__ == "__main__":
+
    if __name__ == "__main__":
-
    main()
+
        main()
</source>
</source>
Line 223: Line 223:
'''Example 6.3. Example of a Hildon picker button'''
'''Example 6.3. Example of a Hildon picker button'''
<source lang="python">
<source lang="python">
-
# Based on C code from:
+
    # Based on C code from:
-
# "Hildon Tutorial" version 2009-04-28
+
    # "Hildon Tutorial" version 2009-04-28
-
# Example 6.3, "Example of a Hildon picker button"
+
    # Example 6.3, "Example of a Hildon picker button"
-
 
+
   
-
import gtk
+
    import gtk
-
import hildon
+
    import hildon
-
import gobject
+
    import gobject
-
 
+
   
-
def on_picker_value_changed(button, user_data=None):
+
    def on_picker_value_changed(button, user_data=None):
-
    print "Newly selected value: %s\n" % button.get_value()
+
        print "Newly selected value: %s\n" % button.get_value()
-
 
+
   
-
def app_quit(widget, data=None):
+
    def app_quit(widget, data=None):
-
    gtk.main_quit()
+
        gtk.main_quit()
-
 
+
   
-
def create_customized_selector():
+
    def create_customized_selector():
-
    # Create a touch selector  
+
        # Create a touch selector  
-
    selector = hildon.TouchSelector()
+
        selector = hildon.TouchSelector()
-
 
+
      
-
     # Stock icons will be used for the example
+
        # Stock icons will be used for the example
-
    icon_list = gtk.stock_list_ids()
+
        icon_list = gtk.stock_list_ids()
 +
       
 +
        print icon_list
 +
   
 +
        # Create model to store selector's items
 +
        store_icons = gtk.ListStore(gobject.TYPE_STRING)
 +
   
 +
        # Populate model
 +
        for item in icon_list:
 +
            new_iter = store_icons.append()
 +
            store_icons.set_value(new_iter, 0, item)
 +
   
 +
        # Create and set up a text renderer to use in the selector
 +
        renderer = gtk.CellRendererPixbuf()
 +
        renderer.set_fixed_size(-1, 100)
 +
   
 +
        # Add the column to the selector
 +
        column = selector.append_column(store_icons, renderer, stock_id=0)
      
      
-
    print icon_list
+
         # Set the selection mode
-
 
+
        selector.set_column_selection_mode(hildon.TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)
-
    # Create model to store selector's items
+
   
-
    store_icons = gtk.ListStore(gobject.TYPE_STRING)
+
        # Set the property "text-column" that indicates the column
-
 
+
        # of the model to get the string from
-
    # Populate model
+
        column.set_property("text-column", 0)
-
    for item in icon_list:
+
      
-
         new_iter = store_icons.append()
+
        return selector
-
        store_icons.set_value(new_iter, 0, item)
+
   
-
 
+
    def main ():
-
    # Create and set up a text renderer to use in the selector
+
        program = hildon.Program.get_instance()
-
    renderer = gtk.CellRendererPixbuf()
+
        gtk.set_application_name("hildon-touch-selector example program")
-
    renderer.set_fixed_size(-1, 100)
+
      
-
 
+
        window = hildon.StackableWindow()
-
    # Add the column to the selector
+
        program.add_window(window)
-
    column = selector.append_column(store_icons, renderer, stock_id=0)
+
      
-
 
+
        # Create touch selector
-
    # Set the selection mode
+
        selector = create_customized_selector()
-
    selector.set_column_selection_mode(hildon.TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)
+
      
-
 
+
        # Create a picker button
-
    # Set the property "text-column" that indicates the column
+
        picker_button = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,
-
    # of the model to get the string from
+
                                            hildon.BUTTON_ARRANGEMENT_VERTICAL)
-
    column.set_property("text-column", 0)
+
      
-
 
+
        # Set a title to the button  
-
     return selector
+
        picker_button.set_title("Select an item")
-
 
+
      
-
def main ():
+
        # Attach the touch selector to the picker button
-
    program = hildon.Program.get_instance()
+
        picker_button.set_selector(selector)
-
    gtk.set_application_name("hildon-touch-selector example program")
+
      
-
 
+
        # Attach callback to the "value-changed" signal
-
     window = hildon.StackableWindow()
+
        picker_button.connect("value-changed", on_picker_value_changed)
-
    program.add_window(window)
+
      
-
 
+
        # Add button to main window
-
     # Create touch selector
+
        window.add(picker_button)
-
    selector = create_customized_selector()
+
      
-
 
+
        window.connect("destroy", app_quit)
-
     # Create a picker button
+
        window.show_all()
-
    picker_button = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,
+
        gtk.main()
-
                                        hildon.BUTTON_ARRANGEMENT_VERTICAL)
+
   
-
 
+
    if __name__ == "__main__":
-
     # Set a title to the button  
+
        main()
-
    picker_button.set_title("Select an item")
+
-
 
+
-
     # Attach the touch selector to the picker button
+
-
    picker_button.set_selector(selector)
+
-
 
+
-
     # Attach callback to the "value-changed" signal
+
-
    picker_button.connect("value-changed", on_picker_value_changed)
+
-
 
+
-
     # Add button to main window
+
-
    window.add(picker_button)
+
-
 
+
-
     window.connect("destroy", app_quit)
+
-
    window.show_all()
+
-
    gtk.main()
+
-
 
+
-
if __name__ == "__main__":
+
-
    main()
+
</source>
</source>
In the above example a picker button is created. The reference to the attached selector is stored in the property "touch-selector" of the picker button. To retrieve the attached selector, use function <code>hildon.PickerButton()</code>. To attach the selector, use the function <code>picker_button.set_selector()</code>.
In the above example a picker button is created. The reference to the attached selector is stored in the property "touch-selector" of the picker button. To retrieve the attached selector, use function <code>hildon.PickerButton()</code>. To attach the selector, use the function <code>picker_button.set_selector()</code>.
Line 327: Line 327:
'''Example 6.4. Example of a Hildon picker button with a selector entry'''
'''Example 6.4. Example of a Hildon picker button with a selector entry'''
<source lang="python">
<source lang="python">
-
# Based on C code from:
+
    # Based on C code from:
-
# "Hildon Tutorial" version 2009-04-28
+
    # "Hildon Tutorial" version 2009-04-28
-
# Example 6.4, "Hildon picker button with a selector entry"
+
    # Example 6.4, "Hildon picker button with a selector entry"
-
 
+
   
-
import gtk
+
    import gtk
-
import hildon
+
    import hildon
-
 
+
   
-
def app_quit(widget, data=None):
+
    def app_quit(widget, data=None):
-
    gtk.main_quit()
+
        gtk.main_quit()
-
 
+
   
-
def main ():
+
    def main ():
-
    artists = [
+
        artists = [
-
        "AC/DC",
+
            "AC/DC",
-
        "Aerosmith",
+
            "Aerosmith",
-
        "Alice in Chains",
+
            "Alice in Chains",
-
        "Black Sabbath",
+
            "Black Sabbath",
-
        "Carcass",
+
            "Carcass",
-
        "Danzig",
+
            "Danzig",
-
        "Deep Purple",
+
            "Deep Purple",
-
        "Dream Theater",
+
            "Dream Theater",
-
        "Eric Clapton",
+
            "Eric Clapton",
-
    ]
+
        ]
-
 
+
      
-
     program = hildon.Program.get_instance()
+
        program = hildon.Program.get_instance()
-
    gtk.set_application_name("hildon-touch-selector example program")
+
        gtk.set_application_name("hildon-touch-selector example program")
-
 
+
      
-
     window = hildon.StackableWindow()
+
        window = hildon.StackableWindow()
-
    program.add_window(window)
+
        program.add_window(window)
-
 
+
      
-
     # Create a picker button
+
        # Create a picker button
-
    picker_button = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,
+
        picker_button = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,
-
                                        hildon.BUTTON_ARRANGEMENT_VERTICAL)
+
                                            hildon.BUTTON_ARRANGEMENT_VERTICAL)
-
 
+
      
-
     # Set a title to the button  
+
        # Set a title to the button  
-
    picker_button.set_title("Pick a band!")
+
        picker_button.set_title("Pick a band!")
-
 
+
      
-
     # Create a touch selector entry
+
        # Create a touch selector entry
-
    selector = hildon.TouchSelectorEntry(text=True)
+
        selector = hildon.TouchSelectorEntry(text=True)
-
     
+
         
-
    # Populate the selector
+
        # Populate the selector
-
    for artist in artists:
+
        for artist in artists:
-
        selector.append_text(artist)
+
            selector.append_text(artist)
-
 
+
      
-
     # Attach the touch selector to the picker button
+
        # Attach the touch selector to the picker button
-
    picker_button.set_selector(selector)
+
        picker_button.set_selector(selector)
-
 
+
      
-
     # Add button to main window
+
        # Add button to main window
-
    window.add(picker_button)
+
        window.add(picker_button)
-
 
+
      
-
     window.connect("destroy", app_quit)
+
        window.connect("destroy", app_quit)
-
    window.show_all()
+
        window.show_all()
-
    gtk.main()
+
        gtk.main()
-
 
+
   
-
if __name__ == "__main__":
+
    if __name__ == "__main__":
-
    main()
+
        main()
</source>
</source>
As you can see in the example above, the use of this widget is similar to using a normal touchable selector.
As you can see in the example above, the use of this widget is similar to using a normal touchable selector.
Line 397: Line 397:
'''Example 6.5. Example of a Hildon date button'''
'''Example 6.5. Example of a Hildon date button'''
<source lang="python">
<source lang="python">
-
# Based on C code from:
+
    # Based on C code from:
-
# "Hildon Tutorial" version 2009-04-28
+
    # "Hildon Tutorial" version 2009-04-28
-
# Example 6.5, "Example of a Hildon date button"
+
    # Example 6.5, "Example of a Hildon date button"
-
 
+
   
-
import gtk
+
    import gtk
-
import hildon
+
    import hildon
-
 
+
   
-
def app_quit(widget, data=None):
+
    def app_quit(widget, data=None):
-
    gtk.main_quit()
+
        gtk.main_quit()
-
 
+
   
-
def main ():
+
    def main ():
-
    program = hildon.Program.get_instance()
+
        program = hildon.Program.get_instance()
-
    gtk.set_application_name("hildon-touch-selector example program")
+
        gtk.set_application_name("hildon-touch-selector example program")
-
 
+
      
-
     window = hildon.StackableWindow()
+
        window = hildon.StackableWindow()
-
    program.add_window(window)
+
        program.add_window(window)
-
 
+
      
-
     # Create a date picker
+
        # Create a date picker
-
    date_button = hildon.DateButton(gtk.HILDON_SIZE_AUTO,
+
        date_button = hildon.DateButton(gtk.HILDON_SIZE_AUTO,
-
                                    hildon.BUTTON_ARRANGEMENT_VERTICAL)
+
                                        hildon.BUTTON_ARRANGEMENT_VERTICAL)
-
 
+
      
-
     # Set a title to the button
+
        # Set a title to the button
-
    date_button.set_title("Select an item")
+
        date_button.set_title("Select an item")
-
 
+
      
-
     # Add button to main window  
+
        # Add button to main window  
-
    window.add(date_button)
+
        window.add(date_button)
-
 
+
   
-
       
+
           
-
    window.connect("destroy", app_quit)
+
        window.connect("destroy", app_quit)
-
    window.show_all()
+
        window.show_all()
-
    gtk.main()
+
        gtk.main()
-
 
+
   
-
if __name__ == "__main__":
+
    if __name__ == "__main__":
-
    main()
+
        main()
</source>
</source>
[[Category:Python]]
[[Category:Python]]

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)

Templates used on this page: