1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
from dataclasses import dataclass
import tkinter as tk
from tkinter import ttk
from tkinter.font import Font as tkFont
class MyWindow(tk.Tk):
def __init__(self):
super().__init__()
self.tableInfo = {'Localisation': {
'etc': AtribDef(), 'mac': AtribDef(), 'doc': AtribDef()}}
self.tableInfo['Localisation']['mac'].pk = True
self.tableInfo['Localisation']['doc'].pk = True
self.tableInfo['Localisation']['doc'].unique = True
self.creer_fMain()
self.mainloop()
def creer_fMain(self):
# Propriété représentation atribut
self.atFont = {} # Dictionnaire {Table : {Atribut : Font}}
self.atFontDef = tkFont( # Définition police atribut par defaut
family="Calibri",
weight=tk.font.NORMAL,
slant=tk.font.ROMAN,
overstrike=0,
underline=0,
size=10)
# Generation value
self.creer_atFont('Localisation')
self.creer_vt('Localisation', self)
def creer_atFont(self, table):
self.atFont[table] = {}
self.style = ttk.Style(self)
for a in tuple(self.tableInfo[table].keys()):
self.atFont[table][a] = tkFont()
self.atFont[table][a].font = self.atFontDef.copy()
if self.tableInfo[table][a].pk:
self.atFont[table][a].config(underline=True)
if self.tableInfo[table][a].unique:
self.atFont[table][a].config(weight=tk.font.BOLD)
if self.tableInfo[table][a].fbk is not None:
self.atFont[table][a].config(slant=tk.font.ITALIC)
# self.style.configure(f'style_{table}_{a}', font=self.atFont[table][a].font)
def creer_vt(self, table, frame):
ltatrib = tuple(self.tableInfo['Localisation'].keys())
lvalues = tuple(self.tableInfo['Localisation'].values())
frametab = tk.LabelFrame(frame, text=table, height=5)
frametab.pack(side=tk.LEFT)
tab = ttk.Treeview(frametab, columns=list(
ltatrib), show='headings', height=5)
tab.pack(side=tk.LEFT, fill='x')
style = ttk.Style()
style.theme_use("default")
style.map("Treeview")
for i in ltatrib:
tab.tag_configure(f'style_{table}_{i}',
font=self.atFont[table][i].font)
tab.heading(
column=f'{i}',
text=f'{i}',
# tag=f'style_{table}_{i}',
anchor='w')
tab.column(
column=f'{i}',
width=50,
minwidth=10,
stretch=False)
# style.configure('my.Treeview.Heading', background='gray', font=('Arial Bold', 10))
# lvalues = self.baseCon.exereq( """SELECT * FROM """ + table + """;""")
print(f'tab.heading.dict: {tab.heading.__dict__}')
for value in lvalues:
tab.insert('', 'end', values=value)
# self.update_mess(str(lvalues))
sb = tk.Scrollbar(frametab, orient=tk.VERTICAL)
sb.pack(side=tk.RIGHT, fill=tk.Y)
tab.config(yscrollcommand=sb.set)
sb.config(command=tab.yview)
return
@ dataclass
class AtribDef (object):
"Definition d'un atribut"
type: str = 'bool'
notnull: bool = 'False'
dflt_value: str = ''
pk: int = 0
fbk: tuple = None # tuple(table, atribut) origine clée etrangere
unique: bool = False
def __repr__(self):
return f'''AtribDef(
type={self.type},
notnull={self.notnull},
dflt_value={self.dflt_value},
pk={self.pk},
fbk={self.fbk},
unique={self.unique})'''
app = MyWindow()
app.mainloop() |
Partager