|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="treeview_advanced_liststore.py" xml:lang="el">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Απλή προβολή οθόνης (Treeview) με αποθήκευση καταλόγου (ListStore) (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#treeview"/>
|
|
Packit |
1470ea |
<link type="next" xref="treeview_cellrenderertoggle.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-06-30" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Marta Maria Casetti</name>
|
|
Packit |
1470ea |
<email its:translate="no">mmcasetti@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>Μια TreeView που εμφανίζει μια TreeStore (πιο σύνθετο παράδειγμα)</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Ελληνική μεταφραστική ομάδα GNOME</mal:name>
|
|
Packit |
1470ea |
<mal:email>team@gnome.gr</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012-2015</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Δημήτρης Σπίγγος</mal:name>
|
|
Packit |
1470ea |
<mal:email>dmtrs32@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012, 2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Μαρία Θουκιδίδου</mal:name>
|
|
Packit |
1470ea |
<mal:email>marablack3@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2014</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Θάνος Τρυφωνίδης</mal:name>
|
|
Packit |
1470ea |
<mal:email>tomtryf@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2014, 2015</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Πιο σύνθετη προβολή δένδρου με ListStore</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/treeview_advanced_liststore.png"/>
|
|
Packit |
1470ea |
Αυτή η TreeView εμφανίζει μια απλή ListStore με το συνδεμένο σήμα επιλογής "αλλαγμένο".
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Ο χρησιμοποιούμενος κώδικας για παραγωγή αυτού παραδείγματος</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
from gi.repository import Pango
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
list_of_dvd = [["The Usual Suspects"],
|
|
Packit |
1470ea |
["Gilda"],
|
|
Packit |
1470ea |
["The Godfather"],
|
|
Packit |
1470ea |
["Pulp Fiction"],
|
|
Packit |
1470ea |
["Once Upon a Time in the West"],
|
|
Packit |
1470ea |
["Rear Window"]]
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="My DVDs", application=app)
|
|
Packit |
1470ea |
self.set_default_size(250, 100)
|
|
Packit |
1470ea |
self.set_border_width(10)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the data are stored in the model
|
|
Packit |
1470ea |
# create a liststore with one column
|
|
Packit |
1470ea |
self.listmodel = Gtk.ListStore(str)
|
|
Packit |
1470ea |
for i in range(len(list_of_dvd)):
|
|
Packit |
1470ea |
self.listmodel.append(list_of_dvd[i])
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a treeview to see the data stored in the model
|
|
Packit |
1470ea |
view = Gtk.TreeView(model=self.listmodel)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# cellrenderer for the first column
|
|
Packit |
1470ea |
cell = Gtk.CellRendererText()
|
|
Packit |
1470ea |
# the first column is created
|
|
Packit |
1470ea |
col = Gtk.TreeViewColumn("Title", cell, text=0)
|
|
Packit |
1470ea |
# and it is appended to the treeview
|
|
Packit |
1470ea |
view.append_column(col)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# when a row of the treeview is selected, it emits a signal
|
|
Packit |
1470ea |
self.selection = view.get_selection()
|
|
Packit |
1470ea |
self.selection.connect("changed", self.on_changed)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the label we use to show the selection
|
|
Packit |
1470ea |
self.label = Gtk.Label()
|
|
Packit |
1470ea |
self.label.set_text("")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a button to add new titles, connected to a callback function
|
|
Packit |
1470ea |
self.button_add = Gtk.Button(label="Add")
|
|
Packit |
1470ea |
self.button_add.connect("clicked", self.add_cb)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# an entry to enter titles
|
|
Packit |
1470ea |
self.entry = Gtk.Entry()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a button to remove titles, connected to a callback function
|
|
Packit |
1470ea |
self.button_remove = Gtk.Button(label="Remove")
|
|
Packit |
1470ea |
self.button_remove.connect("clicked", self.remove_cb)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a button to remove all titles, connected to a callback function
|
|
Packit |
1470ea |
self.button_remove_all = Gtk.Button(label="Remove All")
|
|
Packit |
1470ea |
self.button_remove_all.connect("clicked", self.remove_all_cb)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid to attach the widgets
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.attach(view, 0, 0, 4, 1)
|
|
Packit |
1470ea |
grid.attach(self.label, 0, 1, 4, 1)
|
|
Packit |
1470ea |
grid.attach(self.button_add, 0, 2, 1, 1)
|
|
Packit |
1470ea |
grid.attach_next_to(
|
|
Packit |
1470ea |
self.entry, self.button_add, Gtk.PositionType.RIGHT, 1, 1)
|
|
Packit |
1470ea |
grid.attach_next_to(
|
|
Packit |
1470ea |
self.button_remove, self.entry, Gtk.PositionType.RIGHT, 1, 1)
|
|
Packit |
1470ea |
grid.attach_next_to(
|
|
Packit |
1470ea |
self.button_remove_all, self.button_remove, Gtk.PositionType.RIGHT, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the grid to the window
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def on_changed(self, selection):
|
|
Packit |
1470ea |
# get the model and the iterator that points at the data in the model
|
|
Packit |
1470ea |
(model, iter) = selection.get_selected()
|
|
Packit |
1470ea |
# set the label to a new value depending on the selection, if there is
|
|
Packit |
1470ea |
# one
|
|
Packit |
1470ea |
if iter is not None:
|
|
Packit |
1470ea |
self.label.set_text("\n %s" % (model[iter][0]))
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
self.label.set_text("")
|
|
Packit |
1470ea |
return True
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for the "Add" button
|
|
Packit |
1470ea |
def add_cb(self, button):
|
|
Packit |
1470ea |
# append to the model the title that is in the entry
|
|
Packit |
1470ea |
title = self.entry.get_text()
|
|
Packit |
1470ea |
self.listmodel.append([title])
|
|
Packit |
1470ea |
# and print a message in the terminal
|
|
Packit |
1470ea |
print("%s has been added" % (title))
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def remove_cb(self, button):
|
|
Packit |
1470ea |
# if there is still an entry in the model
|
|
Packit |
1470ea |
if len(self.listmodel) != 0:
|
|
Packit |
1470ea |
# get the selection
|
|
Packit |
1470ea |
(model, iter) = self.selection.get_selected()
|
|
Packit |
1470ea |
# if there is a selection, print a message in the terminal
|
|
Packit |
1470ea |
# and remove it from the model
|
|
Packit |
1470ea |
if iter is not None:
|
|
Packit |
1470ea |
print("%s has been removed" % (model[iter][0]))
|
|
Packit |
1470ea |
self.listmodel.remove(iter)
|
|
Packit |
1470ea |
# otherwise, ask the user to select something to remove
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
print("Select a title to remove")
|
|
Packit |
1470ea |
# else, if there are no entries in the model, print "Empty list"
|
|
Packit |
1470ea |
# in the terminal
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
print("Empty list")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def remove_all_cb(self, button):
|
|
Packit |
1470ea |
# if there is still an entry in the model
|
|
Packit |
1470ea |
if len(self.listmodel) != 0:
|
|
Packit |
1470ea |
# remove all the entries in the model
|
|
Packit |
1470ea |
for i in range(len(self.listmodel)):
|
|
Packit |
1470ea |
iter = self.listmodel.get_iter(0)
|
|
Packit |
1470ea |
self.listmodel.remove(iter)
|
|
Packit |
1470ea |
# print a message in the terminal alerting that the model is empty
|
|
Packit |
1470ea |
print("Empty list")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Χρήσιμες μέθοδοι για ένα γραφικό στοιχείο TreeView</title>
|
|
Packit |
1470ea |
Το γραφικό στοιχείο TreeView σχεδιάστηκε γύρω από ένα σχέδιο προτύπου/προβολής/ελεγκτή: το πρότυπο αποθηκεύει τα δεδομένα· η Προβολή παίρνει ειδοποιήσεις αλλαγής και εμφανίζει το περιεχόμενο του προτύπου· ο ελεγκτής, τελικά, αλλάζει την κατάσταση του προτύπου και ειδοποιεί την προβολή για αυτές τις αλλαγές. Για περισσότερες πληροφορίες και για μια λίστα χρήσιμων μεθόδων για λίστα TreeModel, δείτε <link xref="model-view-controller.py"/>.
|
|
Packit |
1470ea |
Στη γραμμή 36 το σήμα "changed" συνδέεται με τη συνάρτηση επανάκλησης on_changed() χρησιμοποιώντας widget.connect(signal, callback function) . Δείτε <link xref="signals-callbacks.py"/> για μια πιο λεπτομερή εξήγηση.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>Αναφορές API</title>
|
|
Packit |
1470ea |
Σε αυτό το παράδειγμα χρησιμοποιήσαμε τα παρακάτω:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkTreeView.html">GtkTreeView</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkTreeModel.html">GtkTreeModel</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkListStore.html">GtkListStore</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkCellRendererText.html">GtkCellRendererText</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkTreeViewColumn.html">GtkTreeViewColumn</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://git.gnome.org/browse/pygobject/tree/gi/overrides/Gtk.py">pygobject - δεσμεύσεις Python για αυτοέλεγχο GObject</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|