Python GTK Listviews uses Glade

I am currently developing an application for my Linux desktop that reads data from my Garmin Forerunner sports watch, parses a not-so-well-formed XML file, and writes data to a MySQL database table. I am not too experienced with Python or GTK, so the graphic material that I processed used the graphic designer Glade. Here is the problem. There is some data that does not come from a watch that I would like to add before writing to the database. I read and / or count the number of laps, lap distance, lap speed and lap duration. However, I would like to be able to view each circle on interace and classify the circle as Speedwork, Easy Run, etc. Using combobox. From what I read, a list is the way to go.

However, all the examples and documentation I have seen so far build Listview from code (as opposed to creating it through Glade). I would like to go through my lists (lap [type: int], duration [type: string], distance [type: float] and pace [type: string] --- note, I save time as strings for writing them to the fields time / date in my db), and fill in the fields in the list (which I assume is the right way for this - correct me if I'm wrong) along with the list for categorization. Then I take each line from the list and write it in db.

Does anyone know any examples that might help, or does anyone have any specific thoughts?

Update:

I basically want to know how, if I put a listview or treeview in the GUI via Glade, how I will pack it with the following columns: LapID (int), Distance (float), Duration (String) and combobox, where I could choose, what type of circle was. This is the first part of the battle.

Once I populate the list, how can I refer to each row to write it to the db table?

+3
source share
2 answers

check if the example below will work for you. It loads ui from the glade file and uses the 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, believes

+1
source

, , "column0" "column1" , text=0 text=1 :

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

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

0

Source: https://habr.com/ru/post/1790523/


All Articles