'Efficient code for custom color formatting in tkinter python
So , I was trying to create a Periodic Table and its almost done from the exterior efficiently . However , I couldn't understand if there's any way I could fill in colors in individual buttons in the same fashion . Can anyone please help me regarding this ?
Below here is my code :
from tkinter import *
period_1 = ['H','','','','','','','','','','','','','','','','','He']
period_2 = ['Li','Be','','','','','','','','','','','B','C','N','O','F','Ne']
period_3 = ['Na','Mg','','','','','','','','','','','Al','Si','P','S','Cl','Ar']
period_4 = """K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr""".split(" ")
period_5 = """Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe""".split(" ")
period_6 = """Cs Ba * Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn""".split(" ")
period_6a = """La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu""".split(" ")
period_7 = """Fr Ra ** Rf D Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og""".split(" ")
period_7a = """Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr""".split(" ")
root = Tk()
root.attributes('-fullscreen', True)
root.config(bg='black')
Button(root, text='EXIT', bg='red', fg='white', command=root.destroy).place(x=0, y=0)
canvas_a = Canvas(root, bg='black', width=350, height=50)
canvas_a.place(relx=0.15, rely=0.3)
canvas1 = Canvas(canvas_a, bg='black', width=350, height=50)
canvas1.pack()
canvas2 = Canvas(canvas_a, bg='black', width=350, height=50)
canvas2.pack()
canvas3 = Canvas(canvas_a, bg='black', width=350, height=50)
canvas3.pack()
canvas4 = Canvas(canvas_a, bg='black', width=350, height=50)
canvas4.pack()
canvas5 = Canvas(canvas_a, bg='black', width=350, height=50)
canvas5.pack()
canvas6 = Canvas(canvas_a, bg='black', width=350, height=50)
canvas6.pack()
canvas7 = Canvas(canvas_a, bg='black', width=350, height=50)
canvas7.pack()
canvas_b = Canvas(root, bg='black', width=350, height=50)
canvas_b.place(relx=0.265, rely=0.8)
canvas8 = Canvas(canvas_b, bg='black', width=350, height=50)
canvas8.pack()
canvas9 = Canvas(canvas_b, bg='black', width=350, height=50)
canvas9.pack()
class Table:
def __init__(self):
for i in range(0,18):
Button(canvas1, text=period_1[i], width=6, height=2).pack(side=LEFT)
Button(canvas2, text=period_2[i], width=6, height=2).pack(side=LEFT)
Button(canvas3, text=period_3[i], width=6, height=2).pack(side=LEFT)
Button(canvas4, text=period_4[i], width=6, height=2).pack(side=LEFT)
Button(canvas5, text=period_5[i], width=6, height=2).pack(side=LEFT)
Button(canvas6, text=period_6[i], width=6, height=2).pack(side=LEFT)
Button(canvas7, text=period_7[i], width=6, height=2).pack(side=LEFT)
for i in range(0,15):
Button(canvas8, text=period_6a[i], width=6, height=2).pack(side=LEFT)
Button(canvas9, text=period_7a[i], width=6, height=2).pack(side=LEFT)
table1 = Table()
root.mainloop()
Further Notes : If anyone could tell me how I can color individual buttons (like green to halogens and blue to metals and red to hydrogen) and also to create toplevels for each button command , I would be grateful to him/her .
Solution 1:[1]
I rewrote your code with some better ways to create table. My idea was to pick out the buttons that fell onto a range of type and then loop through those buttons and change its color to those type.
from tkinter import *
period_1 = ['H' ,'','','','','','','','','','','','','','','','','He']
period_2 = ['Li','Be','','','','','','','','','','','B','C','N','O','F','Ne']
period_3 = ['Na','Mg','','','','','','','','','','','Al','Si','P','S','Cl','Ar']
period_4 = """K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr""".split(" ")
period_5 = """Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe""".split(" ")
period_6 = """Cs Ba * Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn""".split(" ")
period_7 = """Fr Ra ** Rf D Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og""".split(" ")
period_6a = """La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu""".split(" ")
period_7a = """Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr""".split(" ")
# Making a list of main elements and secondary elements
main = period_1 + period_2 + period_3 + period_4 + period_5 + period_6 + period_7
sec = period_6a + period_7a
# Colors for each group
non_m_col = '#feab90'
alk_m_col = '#ffe0b2'
alk_ea_col = '#fecc81'
trans_m_col = '#d2c4e8'
halogen_col = '#a4d7a7'
metals_col = '#feab90'
noble_g_col = '#fefffe'
act_col = '#b2e5fd'
rare_m_col = '#e7ee9a'
root = Tk()
# Frame for the entire table
period_tab = Frame(root)
period_tab.pack()
# Frame for the main elements only
main_elem = Frame(period_tab)
main_elem.pack()
# Frame for the secondary elements only
sec_elem = Frame(period_tab)
sec_elem.pack(pady=10)
# Creating a 7x18 table of buttons and appending it to a 2D python list for main elements
buttons = []
for i in range(7):
temp = []
for j in range(18):
but = Button(main_elem,text=main[18*i+j],width=10,bg='#f0f0f0')
but.grid(row=i,column=j)
temp.append(but)
buttons.append(temp)
# Creating a 2x15 table of buttons for secondary elements
for i in range(2):
for j in range(15):
text = sec[15*i+j]
if i == 0: # If row 1 then different color
Button(sec_elem,text=text,width=10,bg=rare_m_col).grid(row=i,column=j)
else:
Button(sec_elem,text=text,width=10,bg=act_col).grid(row=i,column=j)
# Manually pick out main elements from the table
non_metals = buttons[0][0],buttons[1][12:16],buttons[2][13:16],buttons[3][14:16],buttons[4][15]
alk_metals = [row[0] for row in buttons[1:]]
alk_ea_metals = [row[1] for row in buttons[1:]]
halogens = [row[16] for row in buttons[1:]]
noble_gases = [row[-1] for row in buttons[:]]
transition_met = [buttons[x][3:12] for x in range(3,7)]
metals = buttons[6][12:16],buttons[5][12:16],buttons[4][12:15],buttons[3][12:14],buttons[2][12]
rare_metals = [row[2] for row in buttons[3:6]]
actinoid = buttons[-1][2]
plain_but = buttons[0][1:-1],buttons[1][2:12],buttons[2][2:12]
# Change colors for those main element buttons
actinoid['bg'] = act_col
for i in alk_metals: i['bg'] = alk_m_col
for i in alk_ea_metals: i['bg'] = alk_ea_col
for i in halogens: i['bg'] = halogen_col
for i in noble_gases: i['bg'] = noble_g_col
for i in rare_metals: i['bg'] = rare_m_col
for i in transition_met:
for j in i:
j['bg'] = trans_m_col
for i in non_metals:
if isinstance(i,list):
for j in i:
j.config(bg=non_m_col)
else:
i.config(bg=non_m_col)
for i in metals:
if isinstance(i,list):
for j in i:
j.config(bg=metals_col)
else:
i.config(bg=metals_col)
for i in plain_but:
for j in i:
j['relief'] = 'flat'
Button(root,text='EXIT',command=root.destroy).pack(pady=10)
root.mainloop()
I've commented the code to make it more understandable. The slicing part might seem a bit complicated because python list does not support 2D slicing. One way is to create a numpy
array and store the coordinates onto it and then retrieve the respective button object based on coordinate, might be longer code but it would make the slicing more easier and understandable as numpy
supports 2D slicing.
Edit: Here is a more advanced and not so complicated periodic table
Solution 2:[2]
I'd use an object orientated approach and this gives me more flexibility if I want to adjust my code and makes it easier to read and understand what happens. Forgive me for the use of another language in my code, its for personal purpose and I have some trouble with English.
I made the database in 3 lists (symbols/keywords,values) and a dictionary (kategorie_farbe) which can easily moved out in a different file. The list stores all the data that I found on Wikipedia:
symbols = ['H','He','Li','Be','B','C','N','O','F','Ne',
'Na','Mg','Al','Si','P','S','Cl','Ar','K', 'Ca',
'Sc', 'Ti', 'V','Cr', 'Mn', 'Fe', 'Co', 'Ni',
'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr',
'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru',
'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te',
'I', 'Xe','Cs', 'Ba','La', 'Ce', 'Pr', 'Nd', 'Pm',
'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm',
'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir',
'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn',
'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am',
'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr',
'Rf', 'Db', 'Sg', 'Bh','Hs', 'Mt', 'Ds', 'Rg', 'Cn',
'Nh', 'Fl', 'Mc', 'Lv', 'Ts', 'Og']
keywords =['name','index','elementkategorie','gruppe','periode','block',
'atommasse','aggregatzustand','dichte','elektronegativität']
values = [['Wasserstoff',1,'Nichtmetalle',1,1,'s',1.01,'gasförmig',0.08,2.2],#H
['Helium',2,'Edelgase',18,1,'s',4.00,'gasförmig',0.18,'n.A'],#He
['Lithium',3,'Alkalimetalle',1,2,'s',6.94,'fest',0.53,0.98],#Li
['Beryllium',4,'Erdalkalimetalle',2,2,'s',9.01,'fest',1.84,1.57],#Be
['Bor',5,'Halbmetalle',13,2,'p',10.81,'fest',2.46,2.04],#B
['Kohlenstoff',6,'Nichtmetalle',14,2,'p',12.01,'fest',2.26,2.55],#C
['Stickstoff',7,'Nichtmetalle',15,2,'p',14.00,'gasförmig',1.17,3.04],#N
['Sauerstoff',8,'Nichtmetalle',16,2,'p',15.99,'gasförmig',1.43,3.44],#O
['Fluor',9,'Halogene',17,2,'p',18.99,'gasförmig',1.70,3.98],#F
['Neon',10,'Edelgase',18,2,'p',20.17,'gasförmig',0.90,'n.A'],#Ne
['Natrium',11,'Alkalimetalle',1,3,'s',22.99,'fest',0.97,0.93],#Na
['Magnesium',12,'Erdalkalimetalle',2,3,'s',24.31,'fest',1.74,1.31],#Mg
['Aluminium',13,'Metalle',13,3,'p',26.98,'fest',2.69,1.61],#Al
['Silicium',14,'Halbmetalle',14,3,'p',28.08,'fest',2.34,1.90],#Si
['Phosphor',15,'Nichtmetalle',15,3,'p',30.97,'fest',2.4,2.19],#P
['Schwefel',16,'Nichtmetalle',16,3,'p',32.06,'fest',2.07,2.58],#S
['Chlor',17,'Halogene',17,3,'p',35.45,'gasförmig',3.22,3.16],#Cl
['Argon',18,'Edelgase',18,3,'p',39.95,'gasförmig',1.78,'n.A'],#Ar
['Kalium',19,'Alkalimetalle',1,4,'s',39.09,'fest',0.86,0.82],#K
['Calicium',20,'Erdalkalimetalle',2,4,'s',40.08,'fest',1.55,1.00],#Ca
['Scandium',21,'Übergangsmetalle',3,4,'d',44.96,'fest',2.99,1.36],#Sc
['Titan',22,'Übergangsmetalle',4,4,'d',47.87,'fest',4.5,1.54],#Ti
['Vandium',23,'Übergangsmetalle',5,4,'d',50.94,'fest',6.11,1.63],#V
['Chrom',24,'Übergangsmetalle',6,4,'d',51.99,'fest',7.14,1.66],#Cr
['Mangan',25,'Übergangsmetalle',7,4,'d',54.94,'fest',7.43,1.55],#Mn
['Eisen',26,'Übergangsmetalle',8,4,'d',55.85,'fest',7.87,1.83],#Fe
['Cobalt',27,'Übergangsmetalle',9,4,'d',58.93,'fest',8.90,1.88],#Co
['Nickel',28,'Übergangsmetalle',10,4,'d',58.69,'fest',8.90,1.91],#Ni
['Kupfer',29,'Übergangsmetalle',11,4,'d',63.54,'fest',8.92,1.90],#Cu
['Zink',30,'Übergangsmetalle',12,4,'d',65.38,'fest',7.14,1.65],#Zn
['Gallium',31,'Metalle',13,4,'p',69.72,'fest',5.90,1.81],#Ga
['Germanium',32,'Halbmetalle',14,4,'p',72.63,'fest',5.32,2.01],#Ge
['Arsen',33,'Halbmetalle',15,4,'p',74.92,'fest',5.73,2.18],#As
['Selen',34,'Halbmetalle',16,4,'p',78.97,'fest',4.82,2.55],#Se
['Brom',35,'Halogene',17,4,'p',79.90,'flüssig',3.12,2.96],#Br
['Krypton',36,'Edelgase',18,4,'p',83.80,'gasförmig',3.75,3.00],#Kr
['Rubidium',37,'Alkalimetalle',1,5,'s',85.47,'fest',1.53,0.82],#Rb
['Strontium',38,'Erdalkalimetalle',2,5,'s',87.62,'fest',2.63,0.95],#Sr
['Yttrium',39,'Übergangsmetalle',3,5,'d',88.91,'fest',4.47,1.22],#Y
['Zirconium',40,'Übergangsmetalle',4,5,'d',91.22,'fest',6.50,1.33],#Zr
['Niob',41,'Übergangsmetalle',5,5,'d',92.90,'fest',8.57,1.6],#Nb
['Molybdän',42,'Übergangsmetalle',6,5,'d',95.95,'fest',10.28,2.16],#Mo
['Technetium',43,'Übergangsmetalle',7,5,'d',98.90,'fest',11.5,1.9],#Tc
['Ruthenium',44,'Übergangsmetalle',8,5,'d',101.07,'fest',12.37,2.2],#Ru
['Rhodium',45,'Übergangsmetalle',9,5,'d',102.90,'fest',12.38,2.28],#Rh
['Palladium',46,'Übergangsmetalle',10,5,'d',106.42,'fest',11.99,2.20],#Pd
['Silber',47,'Übergangsmetalle',11,5,'d',107.87,'fest',10.49,1.93],#Ag
['cadmium',48,'Übergangsmetalle',12,5,'d',112.41,'fest',8.65,1.69],#Cd
['Indium',49,'Metalle',13,5,'p',114.82,'fest',7.31,1.78],#In
['Zinn',50,'Metalle',14,5,'p',118.71,'fest',5.77,1.96],#Sn
['Antimon',51,'Halbmetalle',15,5,'p',121.76,'fest',6.70,2.05],#Sb
['Tellur',52,'Halbmetalle',16,5,'p',127.60,'fest',6.24,2.10],#Te
['Iod',53,'Halogene',17,5,'p',126.90,'fest',4.94,2.66],#I
['Xenon',54,'Edelgase',18,5,'p',131.29,'gasförmig',5.90,2.6],#Xe
['Caesium',55,'Alkalimetalle',1,6,'s',132.91,'fest',1.90,0.79],#Cs
['Barium',56,'Erdalkalimetalle',2,6,'s',137.33,'fest',3.62,0.89],#Ba
['Lanthan',57,'Übergangsmetalle',3,6,'d',138.90,'fest',6.17,1.1],#La
['Cer',58,'Lanthanoide','La',6,'f',140.12,'fest',6.77,1.12],#Ce
['Praseodym',59,'Lanthanoide','La',6,'f',140.91,'fest',6.48,1.13],#Pr
['Neodym',60,'Lanthanoide','La',6,'f',144.24,'fest',7.00,1.14],#Nd
['Promethium',61,'Lanthanoide','La',6,'f',146.91,'fest',7.2,'n.A.'],#Pm
['Samarium',62,'Lanthanoide','La',6,'f',150.36,'fest',7.54,1.17],#Sm
['Europium',63,'Lanthanoide','La',6,'f',151.96,'fest',5.25,'n.A'],#Eu
['Gadolinium',64,'Lanthanoide','La',6,'f',157.25,'fest',7.89,1.20],#Gd
['Terbium',65,'Lanthanoide','La',6,'f',158.93,'fest',8.25,'n.A'],#Tb
['Dysprosium',66,'Lanthanoide','La',6,'f',162.50,'fest',8.56,1.22],#Dy
['Holmium',67,'Lanthanoide','La',6,'f',164.93,'fest',8.78,1.23],#Ho
['Erbium',68,'Lanthanoide','La',6,'f',167.26,'fest',9.05,1.24],#Er
['Thulium',69,'Lanthanoide','La',6,'f',168.93,'fest',9.32,1.25],#Tm
['Ytterbium',70,'Lanthanoide','La',6,'f',173.05,'fest',6.97,'n.A'],#Yb
['Lutetium',71,'Lanthanoide','La',6,'f',174.97,'fest',9.84,1.27],#Lu
['Hafnium',72,'Übergangsmetalle',4,6,'d',178.49,'fest',13.28,1.3],#Hf
['Tantal',73,'Übergangsmetalle',5,6,'d',180.95,'fest',16.65,1.5],#Ta
['Wolfram',74,'Übergangsmetalle',6,6,'d',183.84,'fest',19.25,2.36],#W
['Rhenium',75,'Übergangsmetalle',7,6,'d',186.21,'fest',21.00,1.9],#Re
['Osmium',76,'Übergangsmetalle',8,6,'d',190.23,'fest',22.59,2.2],#Os
['Irdium',77,'Übergangsmetalle',9,6,'d',192.22,'fest',22.56,2.2],#Ir
['Platin',78,'Übergangsmetalle',10,6,'d',195.08,'fest',21.45,2.2],#Pt
['Gold',79,'Übergangsmetalle',11,6,'d',196.97,'fest',19.32,2.54],#Au
['Quecksilber',80,'Übergangsmetalle',12,6,'d',200.59,'flüssig',13.55,2.00],#Hg
['Thalium',81,'Metalle',13,6,'p',204.38,'fest',11.85,1.62],#Tl
['Blei',82,'Metalle',14,6,'p',207.20,'fest',11.34,2.33],#Pb
['Bismut',83,'Metalle',15,6,'p',208.98,'fest',9.78,2.02],#Bi
['Polonium',84,'Metalle',16,6,'p',209.98,'fest',9.20,2.0],#Po
['Astat',85,'Halogene',17,6,'p',209.99,'fest','n.A',2.2],#At
['Radon',86,'Edelgase',18,6,'p',222.00,'gasförmig',9.73,'n.A'],#Rn
['Francium',87,'Alkalimetalle',1,7,'s',223.02,'fest','n.A',0.7],#Fr
['Radium',88,'Erdalkalimetalle',2,7,'s',226.03,'fest',5.5,0.9],#Ra
['Actinium',89,'Übergangsmetalle',3,7,'d',227.03,'fest',10.07,1.1],#Ac
['Thorium',90,'Actinoide','Ac',7,'f',232.04,'fest',11.72,1.3],#Th
['Protactinium',91,'Actinoide','Ac',7,'f',231.04,'fest',15.37,1.5],#Pa
['Uran',92,'Actinoide','Ac',7,'f',238.03,'fest',19.16,1.38],#U
['Neptunium',93,'Actinoide','Ac',7,'f',237.05,'fest',20.45,1.36],#Np
['Plutonium',94,'Actinoide','Ac',7,'f',244.06,'fest',19.82,1.28],#Pu
['Americium',95,'Actinoide','Ac',7,'f',243.06,'fest',13.67,1.3],#Am
['Curium',96,'Actinoide','Ac',7,'f',247.07,'fest',13.51,1.3],#Cm
['Berkelium',97,'Actinoide','Ac',7,'f',247,'fest',14.78,1.3],#Bk
['Californium',98,'Actinoide','Ac',7,'f',251,'fest',15.1,1.3],#Cf
['Einsteinium',99,'Actinoide','Ac',7,'f',252,'fest',8.84,'n.A'],#Es
['Fermium',100,'Actinoide','Ac',7,'f',257.10,'fest','n.A','n.A'],#Fm
['Medelevium',101,'Actinoide','Ac',7,'f',258,'fest','n.A','n.A'],#Md
['Nobelium',102,'Actinoide','Ac',7,'f',259,'fest','n.A.','n.A'],#No
['Lawrencium',103,'Actinoide','Ac',7,'f',266,'fest','n.A','n.A'],#Lr
['Rutherdordium',104,'Übergangsmetalle',4,7,'d',261.11,'fest',17.00,'n.A'],#Rf
['Dubnium',105,'Übergangsmetalle',5,7,'d',262.11,'n.A','n.A','n.A'],#Db
['Seaborgium',106,'Übergangsmetalle',6,7,'d',263.12,'n.A','n.A','n.A'],#Sg
['Bohrium',107,'Übergangsmetalle',7,7,'d',262.12,'n.A','n.A','n.A'],#Bh
['Hassium',108,'Übergangsmetalle',8,7,'d',265,'n.A','n.A','n.A'],#Hs
['Meitnerium',109,'Unbekannt',9,7,'d',268,'n.A','n.A','n.A'],#Mt
['Darmstadtium',110,'Unbekannt',10,7,'d',281,'n.A','n.A','n.A'],#Ds
['Roentgenium',111,'Unbekannt',11,7,'d',280,'n.A','n.A','n.A'],#Rg
['Copernicium',112,'Unbekannt',12,7,'d',277,'n.A','n.A','n.A'],#Cn
['Nihonium',113,'Unbekannt',13,7,'p',287,'n.A','n.A','n.A'],#Nh
['Flerovium',114,'Unbekannt',14,7,'p',289,'n.A','n.A','n.A'],#Fl
['Moscovium',115,'Unbekannt',15,7,'p',288,'n.A','n.A','n.A'],#Mc
['Livermorium',116,'Unbekannt',16,7,'p',293,'n.A','n.A','n.A'],#Lv
['Tenness',117,'Unbekannt',17,7,'p',292,'n.A','n.A','n.A'],#Ts
['Oganesson',118,'Unbekannt',18,7,'p',294,'fest',6.6,'n.A']#Og
]
kategorie_farben = {'Alkalimetalle' : '#fe6f61',
'Erdalkalimetalle':'#6791a7',
'Übergangsmetalle':'#83b8d0',
'Metalle':'#cae2ed',
'Halbmetalle':'#a7d6bc',
'Nichtmetalle':'#ffde66',
'Halogene':'#e9aa63',
'Edelgase':'#e29136',
'Unbekannt':'#cec0bf',
'Lanthanoide':'#696071',
'Actinoide':'#5b4c68'}
Here I made an object that inherits from tk.Frame
and a few labels gridded on it to show the data of my choice:
class Element(tk.Frame):
la_offset = 2;ac_offset=2;offset=2
def __init__(self,master,symbol,**kwargs):
tk.Frame.__init__(self,master,
relief = 'raised')
self.kwargs = kwargs
self.WIDTH,self.HEIGHT,self.BD = 100,100,3
self.CMP = self.BD*2
bg = kategorie_farben.get(kwargs.get('elementkategorie'))
self.configure(width=self.WIDTH,height=self.HEIGHT,bd=self.BD,
bg=bg)
self.grid_propagate(0)
self.idx = tk.Label(self,text=kwargs.get('index'),bg=bg)
self.u = tk.Label(self,text=kwargs.get('atommasse'),bg=bg)
self.name = tk.Label(self,text=kwargs.get('name'),bg=bg)
self.symb = tk.Label(self,text=symbol,font=('bold'),fg=self.get_fg(),bg=bg)
self.e = tk.Label(self,text=kwargs.get('elektronegativität'),bg=bg)
self.d = tk.Label(self,text=kwargs.get('dichte'),bg=bg)
self.grid_columnconfigure(1, weight=2)
self.grid_rowconfigure(1, weight=2)
self.idx.grid(row=0,column=0,sticky='w')
self.u.grid(row=0,column=2,sticky='e')
mid_x = self.WIDTH/2-self.name.winfo_reqwidth()/2
mid_y = self.HEIGHT/2-self.name.winfo_reqheight()/2
offset= 15
self.name.place(in_=self,x=mid_x-self.CMP,y=mid_y-self.CMP+offset)
mid_x = self.WIDTH/2-self.symb.winfo_reqwidth()/2
mid_y = self.HEIGHT/2-self.symb.winfo_reqheight()/2
self.symb.place(in_=self,x=mid_x-self.CMP,y=mid_y-self.CMP-offset/2)
self.e.grid(row=2,column=0,sticky='w')
self.d.grid(row=2,column=2,sticky='e')
r,c = kwargs.pop('periode'),kwargs.pop('gruppe')
if c in ('La','Ac'):
var = 2
if c is 'La':
c =Element.la_offset+var;Element.la_offset +=1
r += self.offset
if c is 'Ac':
c =Element.ac_offset+var;Element.ac_offset +=1
r += Element.offset
self.grid(row=r,column=c,sticky='nswe')
def get_fg(self):
if self.kwargs.get('aggregatzustand') == 'fest': return 'black'
if self.kwargs.get('aggregatzustand') == 'flüssig': return 'blue'
if self.kwargs.get('aggregatzustand') == 'gasförmig': return 'red'
if self.kwargs.get('aggregatzustand') == 'n.A': return 'grey'
and in the end I made the window and a for loop to run my programm:
import tkinter as tk
root = tk.Tk()
for idx,symbol in enumerate(symbols):
kwargs = {}
for k,v in zip(keywords,values[idx]):
kwargs.update({k:v})
Element(root,symbol,**kwargs)
The final result looks like this:
As a clickable version of that table:
class Element(tk.Frame):
la_offset = 2;ac_offset=2;offset=2
def __init__(self,master,symbol,**kwargs):
tk.Frame.__init__(self,master,
relief = 'raised')
self.kwargs = kwargs
self.command= kwargs.pop('command', lambda:print('No command'))
self.WIDTH,self.HEIGHT,self.BD = 100,100,3
self.CMP = self.BD*2
bg = kategorie_farben.get(kwargs.get('elementkategorie'))
self.configure(width=self.WIDTH,height=self.HEIGHT,bd=self.BD,
bg=bg)
self.grid_propagate(0)
self.idx = tk.Label(self,text=kwargs.get('index'),bg=bg)
self.u = tk.Label(self,text=kwargs.get('atommasse'),bg=bg)
self.name = tk.Label(self,text=kwargs.get('name'),bg=bg)
self.symb = tk.Label(self,text=symbol,font=('bold'),fg=self.get_fg(),bg=bg)
self.e = tk.Label(self,text=kwargs.get('elektronegativität'),bg=bg)
self.d = tk.Label(self,text=kwargs.get('dichte'),bg=bg)
self.grid_columnconfigure(1, weight=2)
self.grid_rowconfigure(1, weight=2)
self.idx.grid(row=0,column=0,sticky='w')
self.u.grid(row=0,column=2,sticky='e')
mid_x = self.WIDTH/2-self.name.winfo_reqwidth()/2
mid_y = self.HEIGHT/2-self.name.winfo_reqheight()/2
offset= 15
self.name.place(in_=self,x=mid_x-self.CMP,y=mid_y-self.CMP+offset)
mid_x = self.WIDTH/2-self.symb.winfo_reqwidth()/2
mid_y = self.HEIGHT/2-self.symb.winfo_reqheight()/2
self.symb.place(in_=self,x=mid_x-self.CMP,y=mid_y-self.CMP-offset/2)
self.e.grid(row=2,column=0,sticky='w')
self.d.grid(row=2,column=2,sticky='e')
r,c = kwargs.pop('periode'),kwargs.pop('gruppe')
if c in ('La','Ac'):
if c == 'La':
c =Element.la_offset+Element.offset;Element.la_offset +=1
r += self.offset
if c == 'Ac':
c =Element.ac_offset+Element.offset;Element.ac_offset +=1
r += Element.offset
self.grid(row=r,column=c,sticky='nswe')
self.bind('<Enter>', self.in_active)
self.bind('<Leave>', self.in_active)
self.bind('<ButtonPress-1>', self.indicate)
self.bind('<ButtonRelease-1>', self.execute)
[child.bind('<ButtonPress-1>', self.indicate) for child in self.winfo_children()]
[child.bind('<ButtonRelease-1>', self.execute) for child in self.winfo_children()]
def in_active(self,event):
if str(event.type) == 'Enter': self.flag = True
if str(event.type) == 'Leave':
self.flag = False;self.configure(relief='raised')
def indicate(self,event):
self.configure(relief='sunken')
def execute(self,event):
if self.flag: self.command();self.configure(relief='raised')
else: self.configure(relief='raised')
def get_fg(self):
if self.kwargs.get('aggregatzustand') == 'fest': return 'black'
if self.kwargs.get('aggregatzustand') == 'flüssig': return 'blue'
if self.kwargs.get('aggregatzustand') == 'gasförmig': return 'red'
if self.kwargs.get('aggregatzustand') == 'n.A': return 'grey'
def test():
print('testing..')
for idx,symbol in enumerate(symbols):
kwargs = {}
for k,v in zip(keywords,values[idx]):
kwargs.update({k:v})
Element(root,symbol,command=test,**kwargs)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |