我想在Gtk.TreeView的表格中显示我的MySQL数据库数据

时间:2015-12-06 14:48:55

标签: python mysql pygtk gtk3

我想在Gtk.TreeView的表格中显示我的MySQL数据库数据。我正在使用Glade& PyGTK的。那么,如何在滚动窗口小部件中的TreeView小部件中显示数据。

Please check the image here to see the Glade UI I've designed

我只是想弄乱这段代码 -

from gi.repository import Gtk

#list of tuples for each software, containing the software name, initial release, and main programming languages used
software_list = [("Fireox", 2002,  "C++"),
                 ("Eclipse", 2004, "Java" ),
                 ("Pitivi", 2004, "Python"),
                 ("Netbeans", 1996, "Java"),
                 ("Chrome", 2008, "C++"),
                 ("Filezilla", 2001, "C++"),
                 ("Bazaar", 2005, "Python"),
                 ("Git", 2005, "C"),
                 ("Linux Kernel", 1991, "C"),
                 ("GCC", 1987, "C"),
                 ("Frostwire", 2004, "Java")]

class TreeViewFilterWindow(Gtk.TreeView):

    def __init__(self):
        Gtk.ScrolledWindow.__init__(scrolledwindow1)
        scrolledwindow1.set_border_width(10)

        #Setting up the self.grid in which the elements are to be positionned
        scrolledwindow1.grid = Gtk.Grid()
        scrolledwindow1.grid.set_column_homogeneous(True)
        scrolledwindow1.grid.set_row_homogeneous(True)
        scrolledwindow1.add(self.grid)

        #Creating the ListStore model
        self.software_liststore = Gtk.ListStore(str, int, str)
        for software_ref in software_list:
            scrolledwindow1.software_liststore.append(list(software_ref))
        scrolledwindow1.current_filter_language = None

        #Creating the filter, feeding it with the liststore model
        scrolledwindow1.language_filter = self.software_liststore.filter_new()
        #setting the filter function, note that we're not using the
        scrolledwindow1.language_filter.set_visible_func(self.language_filter_func)

        #creating the treeview, making it use the filter as a model, and adding the columns
        scrolledwindow1.treeview = Gtk.TreeView.new_with_model(self.language_filter)
        for i, column_title in enumerate(["Software", "Release Year", "Programming Language"]):
            renderer = Gtk.CellRendererText()
            column = Gtk.TreeViewColumn(column_title, renderer, text=i)
            scrolledwindow1.treeview.append_column(column)

        #creating buttons to filter by programming language, and setting up their events
        scrolledwindow1.buttons = list()
        for prog_language in ["Java", "C", "C++", "Python", "None"]:
            button = Gtk.Button(prog_language)
            scrolledwindow1.buttons.append(button)
            button.connect("clicked", self.on_selection_button_clicked)

        #setting up the layout, putting the treeview in a scrollwindow, and the buttons in a row
        scrolledwindow1.scrollable_treelist = Gtk.ScrolledWindow()
        scrolledwindow1.scrollable_treelist.set_vexpand(True)
        scrolledwindow1.grid.attach(self.scrollable_treelist, 0, 0, 8, 10)
        scrolledwindow1.grid.attach_next_to(self.buttons[0], self.scrollable_treelist, Gtk.PositionType.BOTTOM, 1, 1)
        for i, button in enumerate(self.buttons[1:]):
            scrolledwindow1.grid.attach_next_to(button, self.buttons[i], Gtk.PositionType.RIGHT, 1, 1)
        scrolledwindow1.scrollable_treelist.add(self.treeview)

        scrolledwindow1.show_all()

    def language_filter_func(self, model, iter, data):
        """Tests if the language in the row is the one in the filter"""
        if scrolledwindow1.current_filter_language is None or self.current_filter_language == "None":
            return True
        else:
            return model[iter][2] == self.current_filter_language

    def on_selection_button_clicked(self, widget):
        """Called on any of the button clicks"""
        #we set the current language filter to the button's label
        scrolledwindow1.current_filter_language = widget.get_label()
        print("%s language selected!" % self.current_filter_language)
        #we update the filter, which updates in turn the view
        scrolledwindow1.language_filter.refilter()




builder = Gtk.Builder()
builder.add_from_file("soft.glade")

win = builder.get_object("window1")
#win = Gtk.TreeView()
win = TreeViewFilterWindow()
window = builder.get_object("window1")
scrolledwindow1 = builder.get_object("scrolledwindow1")
window.connect("delete-event", Gtk.main_quit)
window.show_all()

Gtk.main()

0 个答案:

没有答案