Python Forum
Switching from tkinter to gtk is difficult!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Switching from tkinter to gtk is difficult!
#1
I'm trying to populate a gtkTreeView with data from an sqlite database. I had no problems doing this with ttk.treeview but the same method doesn't seem to work.

For my first proper Python program I made a really simple bookkeeping program using sqlite and tkinter. It wasn't too far from being finished except there is a lot of repeated code and I felt tkinter wasn't pretty enough. So after nearly a year away from Python I thought I'd start again, this time with gtk and glade.

I've got lots of questions but I'll try to answer them myself!

For now though, I've set myself the simple task of filling a gtktreeview with data from an sqlite database. The treeview is a list of customers - name, address, email, phone etc.

I'm stumped though. With the tkinter version...

I made the sqlite database
# Create database
conn = sq.connect('Bookkeeping_Database.sqlite3')

# Create the database table
cur.execute("""CREATE TABLE IF NOT EXISTS customers (   
                id INTEGER, 
                name TEXT, 
                company TEXT, 
                street TEXT, 
                town TEXT, 
                city TEXT, 
                county TEXT, 
                postcode TEXT, 
                email TEXT, 
                phone INTEGER
                )""") 
Created the treeview, columns and headings
# Add a Treeview to the frame
            self.customer_treeview = ttk.Treeview(self.customer_treeview_frame, yscrollcommand=self.customer_treeview_frame_scroll.set, selectmode="extended")
            self.customer_treeview.pack(fill="both", expand="y")     

            # Create the columns in the customer treeview
            self.customer_treeview['columns'] = (
                "ID", 
                "Name", 
                "Company", 
                "Street", 
                "Town", 
                "City", 
                "County", 
                "Postcode", 
                "Email", 
                "Phone"
                )
            
            # Provide the headings for each column
            self.customer_treeview.column("#0", width=0, stretch="no")
            self.customer_treeview.heading("#0", text="")
            
            self.customer_treeview.column("ID", width=0, stretch="no")
            self.customer_treeview.heading("ID", text="ID")
            
            self.customer_treeview.column("Name", minwidth=25, width=50) 
            self.customer_treeview.heading("Name", text="Name")   
            
            self.customer_treeview.column("Company", minwidth=25, width=50) 
            self.customer_treeview.heading("Company", text="Company")   
            
            self.customer_treeview.column("Street", minwidth=25, width=50) 
            self.customer_treeview.heading("Street", text="Street")   
            
            self.customer_treeview.column("Town", minwidth=25, width=50) 
            self.customer_treeview.heading("Town", text="Town")   
            
            self.customer_treeview.column("City", minwidth=25, width=50) 
            self.customer_treeview.heading("City", text="City")   
            
            self.customer_treeview.column("County", minwidth=25, width=50) 
            self.customer_treeview.heading("County", text="County")   
            
            self.customer_treeview.column("Postcode", minwidth=25, width=50) 
            self.customer_treeview.heading("Postcode", text="Postcode")   
            
            self.customer_treeview.column("Email", minwidth=25, width=50) 
            self.customer_treeview.heading("Email", text="Email")   
            
            self.customer_treeview.column("Phone", minwidth=25, width=50) 
            self.customer_treeview.heading("Phone", text="Phone") 
Populated the treeview...
# Select the rowid and everything in the table and fetch 
        cur.execute("SELECT rowid, * FROM customers ORDER BY name")
        customer_record = cur.fetchall()    

        # For each row in the table, add the data to the Treeview columns
        global count
        count = 0
        for row in customer_record:
            self.customer_treeview.insert(parent='', index='end', iid=count, text='', values=(  
                row[0], # row_id
                row[2], # name
                row[3], # company
                row[4], # street
                row[5], # town
                row[6], # city
                row[7], # county
                row[8], # postcode
                row[9], # email
                row[10] # phone
                ))
             count+=1
I did this for everything... chart of accounts, customers, vendors, configurable journals and ledgers etc.

But with gtk and glade I'm lost. I've created the gtktreeview and added the columns (gtkTreeViewColumn) in glade. The code to create the sqlite database and select the data is the same but how do I show it in the gtktreeview? How does the .glade file link to the .py file?

I've got the gtk window opening up when I run the Python code but the "customer_treeview.insert" method isn't working because "AttributeError: 'Customers' object has no attribute 'customer_treeview'". The gtkTreeView is called "customer_treeview"

I've done lots of DDGing but didn't get anywhere. So much of the info available is orientated towards C, which I don't understand. I also don't understand gtkliststore and GObject... are these involved in what I'm trying to do?

Thank you for any help!
I've got round the "AttributeError: 'Customers' object has no attribute 'customer_treeview'" error by adding...

self.builder = Gtk.Builder()
self.customers_treeview = self.builder.get_object("customers_treeview")
I'm now told that there is no attribute 'insert'.

So I guess the gtkTreeView doesn't have 'insert' like the ttk.treeview? And I need to do it very differently?
Reply
#2
Look up Gtk.TreeViewColumn and follow the docs/tutorial from there.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with GUI screen switching in Python QT5 code greenelephant 1 262 Apr-04-2024, 09:58 PM
Last Post: deanhystad
  [Tkinter] Unable to Access global Variable when switching between frames tziyong 1 3,497 Nov-03-2019, 01:08 AM
Last Post: balenaucigasa
  [Tkinter] Call a function when switching layouts 4096 0 3,547 Sep-22-2019, 07:39 PM
Last Post: 4096
  [PyGUI] Switching between two frames using Tkinter jenkins43 1 4,642 Jul-17-2019, 10:53 AM
Last Post: metulburr
  [Tkinter] switching frames nick123 2 7,933 Apr-18-2019, 04:50 PM
Last Post: francisco_neves2020
  [Tkinter] Why is it so difficult to just ouput an image where I want? jpezz 14 11,654 Apr-07-2019, 05:07 PM
Last Post: jpezz
  Problem with pygame not switching the mouse image Mondaythe1st 6 6,787 Jul-26-2017, 10:53 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020