import tkinter as tk
import connectiondatabase as con
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}
def create_grid(self,connection):
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(connection) # заполняем таблицу
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,connection):
column = 0
connection.cursor.execute("select id,name from proba")
for k in cn.cursor:
self.list_grid.append([])
self.count +=1
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)
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')
cn = con.ConnectionDataBase('base')
cn.verify_connection()
if cn.flag_connection:
fr_grid.create_grid(cn)
tk.mainloop()