简体   繁体   English

使用Glade的Python GTK Listviews

[英]Python GTK Listviews using Glade

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. 我目前正在为我的Linux桌面开发一个应用程序,该应用程序从我的Garmin Forerunner运动手表中读取数据,解析不太完善的XML文件,并将数据写入MySQL数据库表。 I'm not overly experienced with Python or GTK, so the graphical stuff I handled using the Glade GUI designer. 我对Python或GTK没有太多的经验,所以我使用Glade GUI设计器处理的图形化内容。 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. 但是,我希望能够查看每一圈的内容,并使用组合框将膝部分类为Speedwork,Easy Run等。 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). 但是,到目前为止我看到的所有示例和文档都是从代码构建Listview(而不是通过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. 我想遍历我的列表(lap [type:int],duration [type:string],distance [type:float]和pace [type:string] ---注意,我将时间存储为字符串来编写它们到我的数据库中的时间/日期字段),并填充列表视图中的字段(我假设这是正确的方式 - 如果我错了,请纠正我)以及组合框进行分类。 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. 我基本上想知道如果我通过Glade在GUI上放置listview或treeview,我将如何使用以下列打包它:LapID(int),Distance(float),Duration(String)和一个组合框我在哪里可以选择它是什么类型的圈。 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? 一旦我填写列表,我将如何引用每一行将其写入db表?

check if an example below would work for you. 检查下面的示例是否适合您。 It loads ui from the glade file and uses sqlite database to store items. 它从glade文件加载ui并使用sqlite数据库来存储项目。

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): 林间文件(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 希望这有帮助,问候

For those who noticed 'column0' and 'column1' are the same simply change the text=0 to text=1 like the folowing: 对于那些注意到'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 http://www.pygtk.org/docs/pygtk/class-gtktreeviewcolumn.html#constructor-gtktreeviewcolumn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM