def test_table_sort(self): table = Table(self.window, drag_cols=False, sortable=True, columns=list('AB')) table.pack() for i in range(10): table.insert('', 'end', str(i), values=(i, 10 - i)) self.window.update() table.column('A', type=int) table._sort_column('A', True) self.assertEqual(table.get_children(""), tuple(str(i) for i in range(9, -1, -1))) table._sort_column('B', False) self.assertEqual(table.get_children(""), ('9', '0', '8', '7', '6', '5', '4', '3', '2', '1'))
def test_table_drag_col(self): table = Table(self.window, drag_cols=True, columns=list('ABC')) table.pack() for i in range(10): table.insert('', 'end', str(i), values=tuple(a + str(i) for a in 'ABC')) self.window.update() table.event_generate('<ButtonPress-1>', x=10, y=5) self.window.update() self.assertTrue(table._visual_drag.winfo_ismapped()) table.event_generate('<Motion>', x=10 + table.column('#1', 'width'), y=5) table.event_generate('<ButtonRelease-1>') self.assertFalse(table._visual_drag.winfo_ismapped()) self.assertEqual(table['displaycolumns'], ('B', 'A', 'C')) table.event_generate('<ButtonPress-1>', x=table.winfo_width() - 20, y=5) self.window.update() self.assertFalse(table._visual_drag.winfo_ismapped()) table.event_generate('<ButtonPress-1>', x=table.column('#1', 'width') + 10, y=5) self.window.update() self.assertTrue(table._visual_drag.winfo_ismapped()) table.event_generate('<Motion>', x=10, y=5) table.event_generate('<ButtonRelease-1>') self.assertFalse(table._visual_drag.winfo_ismapped()) self.assertEqual(table['displaycolumns'], ('A', 'B', 'C')) table.event_generate('<ButtonPress-1>', x=10, y=5) self.window.update() self.assertTrue(table._visual_drag.winfo_ismapped()) table.event_generate('<Motion>', x=10 + table.column('#1', 'width'), y=5) table.event_generate('<Motion>', x=10 + 2 * table.column('#1', 'width'), y=5) table.event_generate('<ButtonRelease-1>') self.assertFalse(table._visual_drag.winfo_ismapped()) self.assertEqual(table['displaycolumns'], ('B', 'C', 'A')) table.event_generate('<ButtonPress-1>', x=10 + 2 * table.column('#1', 'width'), y=5) self.window.update() self.assertTrue(table._visual_drag.winfo_ismapped()) table.event_generate('<Motion>', x=10 + table.column('#1', 'width'), y=5) table.event_generate('<ButtonRelease-1>') self.assertFalse(table._visual_drag.winfo_ismapped()) self.assertEqual(table['displaycolumns'], ('B', 'A', 'C'))
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'))
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') sx.grid(row=1, column=0, sticky='ew')