[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tkinter: widget to display set returned from database table

On 2019-06-19 23:50, Rich Shepard wrote:
> In a database application I want to design a view for the table rows
> returned from a select statement. Tkinter has a listbox widget and web
> searches suggest that multicolumn listboxes are best based on ttk.Treeview
> widgets, but my understanding of a treeview is to display a hierarchical set
> rather than a simple list.
> Each table has multiple columns and I want to create a view for each table
> that will allow the user to select a row. The SQL select statement will be
> formed from criteria provided in a separate dialog box.
Here's a small example.

# -*- coding: utf-8 -*-
# Example demonstrating a multi-column table using ttk.Treeview and 
import tkinter as tk
import tkinter.ttk as ttk

class TableExample(tk.Tk):
     def __init__(self):

         self.title('Table example')

         headings = ['English', 'Experanto']

         self.grid_rowconfigure(0, weight=1)
         self.grid_rowconfigure(1, weight=0)
         self.grid_columnconfigure(0, weight=1)
         self.grid_columnconfigure(1, weight=0)

         vscrollbar = tk.Scrollbar(self, orient='vertical')
         vscrollbar.grid(row=0, column=1, sticky='ns')

         hscrollbar = tk.Scrollbar(self, orient='horizontal')
         hscrollbar.grid(row=1, column=0, sticky='we')

         self.column_ids = ['#%d' % h for h in range(1, len(headings) + 1)]

         def fix_map(option):
             # Fix for setting text colour for Tkinter 8.6.9
             # From: https://core.tcl.tk/tk/info/509cafafae
             # Returns the style map for 'option' with any styles 
starting with
             # ('!disabled', '!selected', ...) filtered out.

             # style.map() returns an empty list for missing options, so 
             # should be future-safe.
             return [elm for elm in style.map('Treeview', 
query_opt=option) if
               elm[:2] != ('!disabled', '!selected')]

         style = ttk.Style()
         style.map('Treeview', foreground=fix_map('foreground'),

         self._table = ttk.Treeview(self, columns=self.column_ids,
           displaycolumns='#all', show=['headings'],
           yscrollcommand=vscrollbar.set, xscrollcommand=hscrollbar.set)
         self._table.grid(row=0, column=0, sticky='nswe')

         for id, heading in zip(self.column_ids, headings):
             self._table.heading(id, text=heading)


         # Now to fill the table.
         words = [
             ('zero', 'nul'),
             ('one', 'unu'),
             ('two', 'du'),
             ('three', 'tri'),
             ('four', 'kvar'),
             ('five', 'kvin'),

         for row in words:
             # Add a new row.
             row_id = self._table.insert('', 'end')

             # Fill the new row.
             for column_id, entry in zip(self.column_ids, row):
                 self._table.set(row_id, column_id, entry)