4
votes

I am currently developing an application for my Linux desktop that reads data from my Garmin Forerunner sports watch, parses the not-so-well-formed XML file, and writes the data to a MySQL database table. I'm not overly experienced with Python or GTK, so the graphical stuff I handled using the Glade GUI designer. Here's the issue. There is some data that does not come from the watch that I would like to add prior to writing to the database. I read and/or calculate lap number, lap distance, lap pace, and lap duration. However, I would like to be able to view each lap on the interace, and categorize the lap as Speedwork, Easy Run, etc.. using a combobox. From what I've read, a listview is the way to go.

However, all examples and documentation that I've seen so far construct the Listview from code (as opposed to having it built via Glade). I would like to loop through my lists (lap [type: int], duration [type: string], distance [type: float], and pace [type: string] --- note, I store times as strings to write them to time/date fields in my db), and populate the fields in a listview (which I'm assuming is the right-way to do this --- correct me if I'm wrong) along with a combobox to categorize. Then, I would take each row from the listview and write it to the db.

Does anybody know of any examples that could help, or does anyone have any specific thoughts?

Update:

I basically want to know how, if I place a listview or treeview on a GUI via Glade, how I would pack it with the following columns: LapID (int), Distance (float), Duration (String), and a combobox where I could choose what type of lap it was. That's the first part of the battle.

Once I fill the list, how would I refer to each row to write it to a db table?

2
I'm not sure I understand what your question is. Are you asking how to loop through the ListStore and add data at runtime in Glade?ptomato

2 Answers

3
votes

check if an example below would work for you. It loads ui from the glade file and uses sqlite database to store items.

script:

import gtk
import sqlite3

class  ListViewTestApp:
    def __init__(self):
        builder = gtk.Builder()
        builder.add_from_file('listview_test.glade')
        builder.connect_signals(self)

        self.model = builder.get_object('list_items')
        self.list = builder.get_object('items_view')

        column = gtk.TreeViewColumn('column0', gtk.CellRendererText(), text=0)   
        column.set_clickable(True)   
        column.set_resizable(True)   
        self.list.append_column(column)

        column = gtk.TreeViewColumn('column1', gtk.CellRendererText(), text=0)   
        column.set_clickable(True)   
        column.set_resizable(True)   
        self.list.append_column(column)

        self.create_database()
        self.load_list_items()

        window = builder.get_object('main_window')
        window.show_all()

    def create_database(self):
        self.engine = sqlite3.connect(':memory:')
        self.engine.execute('create table test_table ' + \
            '(id INTEGER NOT NULL PRIMARY KEY, test_field0 VARCHAR(30), test_field1 VARCHAR(30))');
        self.add_new_line('test0', 'test000');
        self.add_new_line('test1', 'test111');

    def load_list_items(self):
        self.model.clear()        
        result = self.engine.execute('select * from test_table');
        for row in result: 
            self.model.append([row[1], row[2]])

    def add_new_line(self, test0, test1):
        query = 'INSERT INTO test_table (test_field0, test_field1) VALUES ("{0}", "{1}")'\
            .format(test0, test1)
        self.engine.execute(query)

    def on_load_button_clicked(self, widget):
        self.load_list_items()

    def on_add_line_button_clicked(self, widget):
        id = len(self.model)
        self.add_new_line('new_item{0}'.format(id), 'new__item{0}'.format(id));
        self.load_list_items()

if __name__ == "__main__":
    ListViewTestApp()
    gtk.main()

glade file (listview_test.glade):

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkListStore" id="list_items">
    <columns>
      <!-- column-name column0 -->
      <column type="gchararray"/>
      <!-- column-name column1 -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkWindow" id="main_window">
    <child>
      <object class="GtkFixed" id="fixed1">
        <property name="visible">True</property>
        <child>
          <object class="GtkTreeView" id="items_view">
            <property name="width_request">270</property>
            <property name="height_request">250</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="model">list_items</property>
          </object>
          <packing>
            <property name="x">200</property>
            <property name="y">20</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="load_button">
            <property name="label" translatable="yes">load</property>
            <property name="width_request">100</property>
            <property name="height_request">40</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_load_button_clicked"/>
          </object>
          <packing>
            <property name="x">54</property>
            <property name="y">49</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="add_line_button">
            <property name="label" translatable="yes">add line</property>
            <property name="width_request">100</property>
            <property name="height_request">40</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="xalign">0.41999998688697815</property>
            <property name="yalign">0.46000000834465027</property>
            <signal name="clicked" handler="on_add_line_button_clicked"/>
          </object>
          <packing>
            <property name="x">54</property>
            <property name="y">113</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

hope this helps, regards

0
votes

For those who noticed 'column0' and 'column1' are the same simply change the text=0 to text=1 like the folowing:

column = gtk.TreeViewColumn('column1', gtk.CellRendererText(), text=1)

http://www.pygtk.org/docs/pygtk/class-gtktreeviewcolumn.html#constructor-gtktreeviewcolumn