[pygtk]Set the data in treeview on button click

Vijay Manohar manohar.gujja at gmail.com
Thu Oct 18 15:39:28 WST 2007


Hi All,

I am a newbie to pygtk, and I have been practising the stuff since two
weeks, I have been trying to design UI from executing some AT commands, I
have designed a simple UI for this, and on click of  an Ok button, I want
the data to be pasted on to the treestore,

I am also pasting out my code for comments, as well as suggestions,

#/usr/bin/env python
"""This module handles the gui information for the AT TEST SUITE window"""

import pygtk
pygtk.require("2.0")
import gtk
from os import path
from serialtest import get_file_name

def create_textbox():
    # Creata a text entry for the gtk window to enter the device information
    textbox =3D gtk.Entry()
    return textbox

def create_label():
    # Create a Label before the text entry
    label =3D gtk.Label()
    label.set_text("Device Information:   ")
    return label

def create_menu_items():
    """Method for creating Menu Items, and returns the menu bar items"""


    #create a File Menu
    file_menu =3D gtk.Menu()

    #create sub items under the file item
    run_tests_item =3D gtk.MenuItem("Run Tests")
    quit_item =3D gtk.MenuItem("Quit")

    #now create another menu called as Help
    help_menu =3D gtk.Menu()

    #create the about named menu item
    about_item =3D gtk.MenuItem("About")

    #add menu items to the Menu
    file_menu.append(run_tests_item)
    file_menu.append(quit_item)
    help_menu.append(about_item)

    #Attach the callback signal to the activate Item
    run_tests_item.connect("activate", menuitem_response, "Run Tests")
    quit_item.connect("activate", destroy, "Quit")
    about_item.connect("activate", menuitem_response, "About")

    #now we associate menu with file item
    file_item =3D gtk.MenuItem("File")

    #Now associate file menu items with file item, as their submenu's
    file_item.set_submenu(file_menu)
    help_item =3D gtk.MenuItem("Help")

    #now associate the same for help also
    help_item.set_submenu(help_menu)
    menu_bar =3D gtk.MenuBar()

    #now append the menu items to Menu bar
    menu_bar.append(file_item)
    menu_bar.append(help_item)

    return menu_bar

def create_treeview():
    """Method for creating a treeview, and return treeview with the
columns"""

    # Now call the global treeview widget and set the model for it
    treestore =3D gtk.TreeStore(str)

    treeview =3D gtk.TreeView(treestore)
    # create the TreeViewColumn to display the data
    tvcolumn =3D gtk.TreeViewColumn('Command')
    tvcolumn1 =3D gtk.TreeViewColumn('Output')

    #Now append the data to the model
    gsm_standards =3D ["07.07", "07.05"]
    for standard in gsm_standards:
        standard_expand =3D treestore.append(None, ["GSM %s" %standard])


    # create a CellRendererText to render the data
    cell =3D gtk.CellRendererText()

    # add the cell to the tvcolumn and allow it to expand
    tvcolumn.pack_start(cell, True)

    # Now add the tvcolumn to TreeView
    tvcolumn.add_attribute(cell, 'text', 0)

    # add tvcolumn to treeview
    treeview.append_column(tvcolumn)
    treeview.append_column(tvcolumn1)

    return treeview

def destroy(widget, event, data =3D None):
    """Returns only true when this method is called and this method will
exit the application"""
    gtk.main_quit()
    print "Caught destroy signal, Exiting..."
    return True

def menuitem_response(widget, string):
    """Method prints the information of the object name on the console
    when ever an event is generated on selecting or pressing"""
    print "%s" %string

class attestsuite:
    """The Class ATTestSuite creates the gui widgets and diplay
information"""

    def __init__(self):
        """This Method initiates and creates the Vertical Box, and adds the
items on top of it by calling individual methods"""

        # Create a new window
        window =3D gtk.Window(gtk.WINDOW_TOPLEVEL)
        # Set a handler for the close event, that immediately closes the GTK
window
        window.connect("delete_event", destroy)
        window.set_title("AT TEST SUITE")
        window.set_size_request(400, 400)

        #Create textbox
        self.textbox =3D create_textbox()

        #Create a HBOX and pack the elements
        hbox =3D gtk.HBox(False, 0)

        # Call label method which creates the Label an example:
        label =3D create_label()

        #Now pack the items into the Horizontal box
        hbox.pack_start(label, False, False, 0)
        hbox.pack_start(self.textbox, False, False, 0)

        #create a vetical box
        vbox =3D gtk.VBox(False, 0)

        # Call the MenuBar method and then add that to the vbox
        menu_bar =3D create_menu_items()

        #create buttons Ok, and Cancel
        button_ok =3D gtk.Button("Ok")
        button_ok.connect("clicked", self.ok_button_pressed, self.textbox)
        button_cancel =3D gtk.Button("Cacel")
        button_cancel.connect("clicked", self.cancel_button_pressed,
self.textbox)


        #Create Hbox2 to pack the buttons
        hbox2 =3D gtk.HBox(False, 0)
        hbox2.pack_start(button_ok, False, False, 0)
        hbox2.pack_start(button_cancel, False, False, 0)

        #call the create_treeview method, which creates the treeview and
returns the treeview
        treeview =3D create_treeview()

        vbox.pack_start(menu_bar, False, False, 0)
        vbox.pack_start(hbox, False, False, 0)
        vbox.pack_start(hbox2, False, False, 0)
        vbox.pack_start(treeview, False, False, 0)
        window.add(vbox)
        window.show_all()

    def main(self):
        """Main method"""
        gtk.main()

    def cancel_button_pressed(self, textbox, event):
        self.textbox.set_text("")

    def ok_button_pressed(self, textbox, event):
        text =3D self.textbox.get_text()
        if path.exists(text):
            gsm_0707_data =3D get_file_name(text)
        else:
            print "No such file or directory"


if __name__=3D=3D"__main__":
    widget =3D attestsuite()
    widget.main()


any suggestions and modifications are welcome.

Any help is really appreciated

with regards,
Manohar Gujja
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20071018/564ed2c1/at=
tachment.htm


More information about the pygtk mailing list