使用 Glade 的 Python GTK ListView

标签 python gtk pygtk glade garmin

我目前正在为我的 Linux 桌面开发一个应用程序,它从我的 Garmin Forerunner 运动 watch 读取数据,解析格式不正确的 XML 文件,并将数据写入 MySQL 数据库表。我对 Python 或 GTK 没有太多经验,所以我使用 Glade GUI 设计器处理了图形内容。这就是问题所在。在写入数据库之前,我想添加一些不是来自 watch 的数据。我阅读和/或计算单圈数、单圈距离、单圈配速和单圈持续时间。但是,我希望能够在界面上查看每一圈,并将该圈分类为 Speedwork、Easy Run 等。使用组合框。据我所知, ListView 是可行的方法。

但是,到目前为止,我所看到的所有示例和文档都是从代码构建 Listview(而不是通过 Glade 构建)。我想遍历我的列表(lap [type: int]、duration [type: string]、distance [type: float] 和 pace [type: string] --- 注意,我将时间存储为字符串以写入它们到我的数据库中的时间/日期字段),并填充 ListView 中的字段(我假设这是执行此操作的正确方法——如果我错了请纠正我)以及要分类的组合框。然后,我将从 ListView 中取出每一行并将其写入数据库。

有没有人知道任何可以提供帮助的例子,或者有没有人有任何具体的想法?

更新:

我基本上想知道,如果我通过 Glade 在 GUI 上放置一个 ListView 或 TreeView ,我将如何用以下列打包它: LapID (int)、Distance (float)、Duration (String) 和一个组合框,我可以在其中选择圈的类型。这是战斗的第一部分。

填满列表后,如何引用每一行以将其写入数据库表?

最佳答案

检查下面的示例是否适合您。它从 glade 文件加载 ui 并使用 sqlite 数据库存储项目。

脚本:

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()

空地文件(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>

希望这对你有帮助,问候

关于使用 Glade 的 Python GTK ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4925155/

相关文章:

python - 如何在 Windows 上安装 PyGI(Python Gobject Introspection)?

python - 如何将文件夹中的所有 JPG 文件转换为 PDF 并将它们合并?

python - 检查 Gtk mainloop 是否正在运行

label - 多行 gtk.Label 忽略 xalign=0.5

python - GTK3+ 和 Python 中的线程同步

c - 如何构建我想要的 GTK GUI 应用程序?

python - PyGTK:过滤输入小部件的键盘输入时,在打开大缓冲区时无法正确设置光标位置

python - 单击 pyqt 按钮后立即停止加载页面 selenium

python - python 鸡 : Extending python with the use of a shared library

python - 使用 Matplotlib 在 Python 中绘制时间