2016-12-23 52 views
0

我对GTK和Gnome应用程序开发很新,所以对我的天真歉意。 (我的开发语言是Python)。我想使用ListBox来显示一些数据,并且单个行视图将非常复杂(即由多个不同的小部件组成)。因此,我宁愿不使用TreeView,因为那需要大量的自定义绘图/事件处理。我注意到ListBox有一个bind_model方法,但看起来我不能用它来绑定ListStore模型,即使认为ListStore实现了ListModel接口。有人知道如何做到这一点?PyGi:如何在GTKListStore中使用GTKListBox?

+3

的'Gtk.ListStore'为'Gtk.TreeView's。您需要为'Gtk.ListBox.bind_model()'使用'Gio.ListStore'。 – elya5

回答

0

这是来自我的开源会计程序的精简代码。

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

它可能不会完全回答你的问题,但它确实工作,它显示了一种使用ListBox的方法。对于复杂的设置,ListBox是一个非常好的选择。就我而言,我每秒都在做太多的操作,以至于使Treeviews崩溃。

0

一个简单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()) 
+0

请添加更多关于您的答案的描述和/或信息,以及如何解决问题,以便其他人可以轻松理解,而无需澄清 – koceeng