Example #1
0
    def test_table_get_set(self):
        table = Table(self.window, columns=('A', 'B', 'C'))
        table.pack()
        self.window.update()

        self.assertTrue(table.cget('sortable'))
        self.assertTrue(table['drag_cols'])
        self.assertTrue(table['drag_rows'])
        self.assertEqual(table.cget('columns'), ('A', 'B', 'C'))
        self.assertTrue(table.heading('A', 'command'))

        table.configure(sortable=False)
        self.assertFalse(table._sortable)
        self.assertFalse(table.heading('A', 'command'))
        table.configure(drag_cols=False)
        self.assertFalse(table._drag_cols)
        table.configure(drag_rows=False)
        self.assertFalse(table._drag_rows)
        table.configure(sortable=True)
        self.assertTrue(table._sortable)
        table.configure(drag_cols=True)
        self.assertTrue(table._drag_cols)
        table.configure(drag_rows=True)
        self.assertTrue(table._drag_rows)

        self.assertEqual(table.config('sortable'),
                         ('sortable', table._sortable))
        self.assertEqual(table.config('drag_cols'),
                         ('drag_cols', table._drag_cols))
        self.assertEqual(table.config('drag_rows'),
                         ('drag_rows', table._drag_rows))
Example #2
0
    def test_table_methods(self):
        table = Table(self.window, columns=('A', 'B', 'C'))
        table.pack()
        self.window.update()

        self.assertEqual(table._visual_drag.cget('columns'), ('A', 'B', 'C'))
        table.configure(columns=('E', 'F', 'G'))
        self.assertEqual(table.cget('columns'), ('E', 'F', 'G'))
        self.assertEqual(table._visual_drag.cget('columns'), ('E', 'F', 'G'))

        for i in range(20):
            table.insert('',
                         'end',
                         str(i),
                         values=tuple(str(i) for a in 'EFG'))

        self.assertEqual(table.get_children(""),
                         table._visual_drag.get_children(""))
        self.assertIs(table.column('E', 'type'), str)
        table.column('E', type=int)
        self.assertIs(table.column('E', 'type'), int)
        table.column('E', width=80)
        self.assertEqual(table._visual_drag.column('E', 'width'), 80)
        self.assertIn('type', table.column('G'))

        table.delete('0', '2')
        self.assertFalse('2' in table._visual_drag.get_children(""))
        self.assertEqual(table.get_children(""),
                         table._visual_drag.get_children(""))

        table.detach('1', '7')
        self.assertFalse('1' in table._visual_drag.get_children(""))
        self.assertEqual(table.get_children(""),
                         table._visual_drag.get_children(""))
        table.reattach('1', '', 0)
        self.assertTrue('1' in table._visual_drag.get_children(""))
        self.assertEqual(table.get_children(""),
                         table._visual_drag.get_children(""))

        table.heading('E', text='col E')
        self.assertEqual(table._visual_drag.heading('E', 'text'), 'col E')

        table.item('4', values=('a', 'b', 'c'), text='4')
        self.assertEqual(table._visual_drag.item('4', 'values'),
                         ('a', 'b', 'c'))
        self.assertEqual(table._visual_drag.item('4', 'text'), '4')

        keys = [
            'columns', 'displaycolumns', 'show', 'selectmode', 'height',
            'padding', 'xscrollcommand', 'yscrollcommand', 'takefocus',
            'cursor', 'style', 'class', 'sortable', 'drag_cols'
        ]
        self.assertEqual(sorted(table.keys()), sorted(keys))

        table.move('4', '', 0)
        self.assertEqual(table.get_children('')[0], '4')
        self.assertEqual(table._visual_drag.get_children('')[0], '4')

        table.configure(height=10)
        self.assertEqual(table.bbox('19'), '')
        self.assertEqual(table._visual_drag.bbox('19'), '')

        self.assertEqual(table.set('3', 'F'), '3')
        table.set('3', 'F', 'f')
        self.assertEqual(table._visual_drag.set('3', 'F'), 'f')

        table.set_children("", '4', '7')
        self.assertEqual(table.get_children(""), ('4', '7'))
        self.assertEqual(table._visual_drag.get_children(""), ('4', '7'))
Example #3
0
root = tk.Tk()

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

style = ttk.Style(root)
style.theme_use('alt')
sortable = tk.BooleanVar(root, False)
drag_row = tk.BooleanVar(root, False)
drag_col = tk.BooleanVar(root, False)

columns = ["A", "B", "C", "D", "E", "F", "G"]
table = Table(root, columns=columns, sortable=sortable.get(), drag_cols=drag_col.get(),
              drag_rows=drag_row.get(), height=6)
for col in columns:
    table.heading(col, text=col)
    table.column(col, width=100, stretch=False)

# sort column A content as int instead of strings
table.column('A', type=int)

for i in range(12):
    table.insert('', 'end', iid=i,
                 values=(i, i) + tuple(i + 10 * j for j in range(2, 7)))

# add scrollbars
sx = tk.Scrollbar(root, orient='horizontal', command=table.xview)
sy = tk.Scrollbar(root, orient='vertical', command=table.yview)
table.configure(yscrollcommand=sy.set, xscrollcommand=sx.set)

table.grid(sticky='ewns')