import tkinter as tk class Grid_Frame(tk.Frame): def __init__(self,master=None,cnf={},**kw): tk.Frame.__init__(self,master,cnf,**kw) self.propagate(flag=False) self.canvas_header = tk.Canvas(self,bg = 'blue') self.canvas_body = tk.Canvas(self,bg = 'green') self.canvas_body.bind_all("<MouseWheel>", self._on_mousewheel) self.frame_header = tk.Frame(self.canvas_header) self.frame_body = tk.Frame(self.canvas_body,bg = 'red') self.count = 0 self.select_row = 0 self.bind_all('<KeyPress>',self.key_press) self.coordinates_header = (0,0,0,0) self.list_header = [] # список заголовков таблицы, элементы списка словари, где ключ visible отображать заголовок name имя заголовка self.list_header = [{'id':'id','name':'код','visible':True,'width':25,'bg':'white','height':1,'anchor':'center'}, {'id':'name','name':'Наименование','visible':True,'width':50,'bg':'white','height':1,'anchor':'center'}] self.dict_column = {'id':0,'name':1} self.list_grid = [] self.list_grid.append([]) def create_grid(self): scr_y = tk.Scrollbar(self,orient='vertical') scr_x = tk.Scrollbar(self,orient='horizontal') scr_y.pack(side='right',fill='y') scr_x.pack(side='bottom',fill='x') self.canvas_header.pack(fill='x',side='top') self.frame_header.pack(fill='x',side='top') self.canvas_body.pack(expand='yes',fill='both',side='top') self.frame_body.pack(fill='x',side='top',anchor='center') self.canvas_body.configure(yscrollcommand=scr_y.set) self.canvas_header.configure(xscrollcommand=scr_x.set) scr_y.configure(command=self.canvas_body.yview) scr_x.configure(command=self._xview) self.create_header(self.list_header) # создаем шапку таблицы self.fill_grid() # заполняем таблицу def _xview(self,*arg): if arg[0] == 'scroll': self.canvas_body.xview_scroll(arg[1], arg[2]) self.canvas_header.xview_scroll(arg[1], arg[2]) elif arg[0] == 'moveto': self.canvas_body.xview_moveto(arg[1]) self.canvas_header.xview_moveto(arg[1]) def _on_mousewheel(self,event): self.canvas_body.yview_scroll(-1*int(event.delta/120), "units")#??????? def create_header(self,list_header): column = 0 for field in list_header: if field['visible']!=True: continue lb = tk.Label(self.frame_header,text=field['name'],relief='raised',width=field['width'] ,bg=field['bg'],height=field['height']) lb.grid(row=0,column=column,sticky='nsew') column +=1 self.update() self.canvas_header.create_window(0,0,anchor='nw',window=self.frame_header,tag='header') coordinates_header = self.canvas_header.bbox('all') self.canvas_header.config(scrollregion=coordinates_header,height=coordinates_header[3]) def fill_grid(self): column = 0 f = open('data.txt','w') cn = [(1, 'Один процент') ,(2, 'Два процента') ,(3, 'Три процента') ,(4, 'Четыре прцента') ,(5, 'Пять процентов') ,(6, 'Шесть процентов') ,(7, 'Накопительная 2') ,(8, 'Накопительная № 4') ,(9, 'Накопителная 5') ,(10, 'Накопительная 6') ,(11, '6%') ,(12, 'Три процента_0') ,(13, 'Четыре процента_3') ,(14, 'Пять процентов_3') ,(15, 'Шесть процентов_0')] for k in cn: f.write(str(k)+'\n') self.count +=1 self.list_grid.append([]) for key_column in self.dict_column: index_column = self.dict_column[key_column] lb = tk.Label(self.frame_body,text=k[index_column],relief='raised',anchor='w' ,width=self.list_header[index_column]['width'] ,height = self.list_header[index_column]['height']) lb.grid(row=self.count,column=index_column,sticky='nsew') self.list_grid[self.count].append(lb) f.close() self.update() self.canvas_body.create_window(0,0,anchor='nw',window=self.frame_body,tag='header') coordinates_scrollregion = self.canvas_body.bbox('all') self.canvas_body.config(scrollregion=coordinates_scrollregion,height=coordinates_scrollregion[3]) def key_press(self,event): count = range(0,len(self.dict_column))#количество столбцов if event.keycode == 40:#клавиша управления вниз if self.select_row < self.count: if self.select_row>0 and self.select_row<self.count: for index_count in count: self.list_grid[self.select_row][index_count].configure(bg='white') self.select_row +=1 for index_count in count: self.list_grid[self.select_row][index_count].configure(bg='#8080c0') elif event.keycode == 38:# клавиша управления вверх if self.select_row >1: if self.select_row >1 : for index_count in count: self.list_grid[self.select_row][index_count].configure(bg='white') self.select_row -=1 for index_count in count: self.list_grid[self.select_row][index_count].configure(bg='#8080c0') #==================================================== if __name__ == '__main__': fr_grid = Grid_Frame(bg='blue',width=600,height=200) fr_grid.pack(expand='yes',fill='both',side="left",anchor='ne') fr_grid.create_grid() tk.mainloop()