GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Oct 20, 2014 5:56 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Automatic empty row in GTKTreeView? (PyGObject)
PostPosted: Wed Jul 02, 2014 11:18 am 
Offline
Familiar Face

Joined: Mon Mar 31, 2014 9:59 pm
Posts: 16
I have difficulty with GTKTreeView. I would like to always have an empty row at the bottom of the table, so the user can create a new entry. But the new row always defaults to 0,000 which then produces a data-point at the coordinates (0|0). Is there any way to just have an empty row at the bottom?

Code:
#!/usr/bin/python3

from gi.repository import Gtk
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas

class DataBase():
    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_default_size(800, 500)
        self.box = Gtk.Box()
        self.window.add(self.box)

        self.fig = Figure(figsize=(10,10), dpi=80)
        self.ax = self.fig.add_subplot(111)

        self.canvas = FigureCanvas(self.fig)
        self.box.pack_start(self.canvas, True, True, 0)
       
        self.liststore = Gtk.ListStore(float, float)
        self.treeview = Gtk.TreeView(model=self.liststore)
        self.box.pack_start(self.treeview, False, True, 0)

        self.xrenderer = Gtk.CellRendererText()
        self.xrenderer.set_property("editable", True)
        self.column_text = Gtk.TreeViewColumn("x-Value", self.xrenderer, text=0)
        self.treeview.append_column(self.column_text)
       
        self.yrenderer = Gtk.CellRendererText()
        self.yrenderer.set_property("editable", True)
        self.column_editabletext = Gtk.TreeViewColumn("y-Value", self.yrenderer, text=1)
        self.treeview.append_column(self.column_editabletext)

        self.xrenderer.connect("edited", self.xedited)
        self.yrenderer.connect("edited", self.yedited)

        self.liststore.append([2.35, 2.40])
        self.liststore.append([3.45, 4.70])
        self.liststore.append()

    def resetplot(self):
        self.ax.cla()
        self.ax.set_xlim(0,10)
        self.ax.set_ylim(0,10)
        self.ax.grid(True)

    def plotpoints(self):
        self.resetplot()
        for row in self.liststore:
            self.ax.scatter(row[:1], row[1:], marker='o', s=50)
        self.fig.canvas.draw()

    def xedited(self, widget, path, number):
        self.liststore[path][0] = float(number.replace(',', '.'))
        self.plotpoints()
       
    def yedited(self, widget, path, number):
        self.liststore[path][1] = float(number.replace(',', '.'))
        self.plotpoints()

db = DataBase()
db.resetplot()
db.plotpoints()

db.window.connect("delete-event", Gtk.main_quit)
db.window.show_all()
Gtk.main()

_________________
I am learning to develop GTK+ (GTK 3.10) programs written in Python 3.4.0, designed with Glade 3.16.1, with plots using Matplotlib 1.4. I program and test using Ubuntu 14.04 (64bit) and Gedit. (http://gtk3-matplotlib-cookbook.readthedocs.org)


Top
 Profile  
 
 Post subject: Re: Automatic empty row in GTKTreeView? (PyGObject)
PostPosted: Tue Aug 05, 2014 12:04 am 
Offline

Joined: Mon Aug 04, 2014 1:20 am
Posts: 2
When using treeviews I usually store all displayed data in the model as strings, mainly because the data retrieved from a database table may include Null values. Inserting the data as string will allow you to insert empty strings to create an empty row like you want.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group