简体   繁体   English

PyGi:如何在GTKListStore中使用GTKListBox?

[英]PyGi: How to use a GTKListBox with a GTKListStore?

I am very new to GTK and Gnome app development, so apologies for my naiveté. 我对GTK和Gnome应用程序开发非常陌生,因此为我的天真致歉。 (My development language is Python). (我的开发语言是Python)。 I would like to use a ListBox to display some data, and the individual row views will be quite complicated (ie composed of multiple different widgets). 我想使用ListBox显示一些数据,并且各个行视图将非常复杂(即由多个不同的小部件组成)。 As a result I would prefer not to use a TreeView , because that will require a bunch of custom drawing/event handling. 结果,我宁愿不使用TreeView ,因为这将需要一堆自定义绘图/事件处理。 I noticed that ListBox has a bind_model method, but it appears I can't use it to bind a ListStore model, even thought ListStore implements the ListModel interface. 我注意到ListBox有一个bind_model方法,但似乎我不能用它来绑定ListStore模型,即使以为ListStore实现了ListModel接口也是如此。 Does anybody know how to accomplish this? 有人知道如何做到这一点吗?

This is condensed code from my open source accounting program. 这是我的开源会计程序中的压缩代码。

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys


class GUI :

    def __init__(self):

        listbox = Gtk.ListBox()

        employee_name_label = Gtk.Label("Henry", xalign=1)

        combo = Gtk.ComboBoxText()
        combo.set_property("can-focus", True)
        for name in ["bar", "foo", "python"]:
            combo.append('0', name)

        list_box_row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=5)
        list_box_row.add(hbox)

        switch = Gtk.Switch()
        switch.props.valign = Gtk.Align.CENTER


        project_time_label = Gtk.Label("0:00:00", xalign=1 )
        project_time_label.set_property('width-chars', 8)


        hbox.pack_start(employee_name_label, True, False, 5)
        hbox.pack_end(project_time_label, False, False, 5)
        hbox.pack_end(switch, False, False, 5)
        hbox.pack_end(combo, False, False, 5)


        listbox.add(list_box_row)

        window = Gtk.Window()
        window.add(listbox)
        window.connect("destroy", self.on_window_destroy)
        window.show_all()

    def on_window_destroy(self, window):
        Gtk.main_quit()

def main():
    app = GUI()
    Gtk.main()

if __name__ == "__main__":
    sys.exit(main())

It may not answer your question exactly, but it does work and it shows a way to use ListBox. 它可能无法完全回答您的问题,但它确实可以工作,并且显示了使用ListBox的方法。 ListBox is a very good choice for complicated setups. 对于复杂的设置,ListBox是一个很好的选择。 In my case I was doing so much operations every second that it crashed Treeviews. 以我为例,我每秒都要进行大量操作,以致崩溃了Treeviews。

A simple exampe: 一个简单的例子:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GObject
import sys

class Item(GObject.GObject):

    text = GObject.property(type = str)

    def __init__(self):
        GObject.GObject.__init__(self)

class GUI:

    def __init__(self):        
        item1 = Item()
        item1.text = "Hello"
        item2 = Item()
        item2.text = "World"

        liststore = Gio.ListStore()
        liststore.append(item1)
        liststore.append(item2)

        listbox=Gtk.ListBox()
        listbox.bind_model(liststore, self.create_widget_func)

        window = Gtk.Window()
        window.add(listbox)
        window.connect("destroy", self.on_window_destroy)
        window.show_all()

    def create_widget_func(self,item):
        label=Gtk.Label(item.text)
        return label

    def on_window_destroy(self, window):
        Gtk.main_quit()

def main():

    app = GUI()
    Gtk.main()

if __name__ == "__main__":
    sys.exit(main())

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

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