1
\$\begingroup\$

This is a code that generates the periodic table where each element is represented as a button and info about the element is produced when that specific button is pressed. I wanted to make this code shorter by changing the way the buttons (elements) for each column of the periodic table are generated. Plus, how to add functionality so that I can toggle between the buttons by pressing the arrow keys? Is there a simpler, shorter, way of generating the buttons in this code?

import tkinter as tk
# Creates and Initiates class 'App'


class App(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.winfo_toplevel().title("Periodic Table of the Elements")
        self.topLabel = tk.Label(self, text="Click the element you would like information about.", font=20)
        self.topLabel.grid(row=0, column=0, columnspan=18)


        # Names of tk.Buttons in column 1
        column1 = [
            ('H', 'Hydrogen', 'Atomic # = 1\nAtomic Weight =1.01\nState = Gas\nCategory = Alkali Metals'),
            ('Li', 'Lithium', 'Atomic # = 3\nAtomic Weight = 6.94\nState = Solid\nCategory = Alkali Metals'),
            ('Na', 'Sodium', 'Atomic # = 11\nAtomic Weight = 22.99\nState = Solid\nCategory = Alkali Metals'),
            ('K', 'Potassium', 'Atomic # = 19\nAtomic Weight = 39.10\nState = Solid\nCategory = Alkali Metals'),
            ('Rb', 'Rubidium', 'Atomic # = 37\nAtomic Weight = 85.47\nState = Solid\nCategory = Alkali Metals'),
            ('Cs', 'Cesium', 'Atomic # = 55\nAtomic Weight = 132.91\nState = Solid\nCategory = Alkali Metals'),
            ('Fr', 'Francium', 'Atomic # = 87\nAtomic Weight = 223.00\nState = Solid\nCategory = Alkali Metals')]
        # create all tk.Buttons with a loop
        r = 1
        c = 0
        for b in column1: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="grey",
                      command=lambda text=b:  self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column2 = [
            ('Be', 'Beryllium', 'Atomic # = 4\nAtomic Weight = 9.01\nState = Solid\nCategory = Alkaline Earth Metals'),
            ('Mg', 'Magnesium', 'Atomic # = 12\nAtomic Weight = 24.31\nState = Solid\nCategory = Alkaline Earth Metal'),
            ('Ca', 'Calcium', 'Atomic # = 20\nAtomic Weight = 40.08\nState = Solid\nCategory = Alkaline Earth Metals'),
            ('Sr', 'Strontium', 'Atomic # = 38\nAtomic Weight = 87.62\nState = Solid\nCategory = Alkaline Earth Metal'),
            ('Ba', 'Barium', 'Atomic # = 56\nAtomic Weight = 137.33\nState = Solid\nCategory = Alkaline Earth Metals'),
            ('Ra', 'Radium', 'Atomic # = 88\nAtomic Weight = 226.03\nState = Solid\nCategory = Alkaline Earth Metals')]
        r = 2
        c = 1
        for b in column2: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light green",
                      command=lambda text=b:  self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column3 = [
            ('Sc', 'Scandium', 'Atomic # = 21\nAtomic Weight = 44.96\nState = Solid\nCategory = Trans Metals'),
            ('Y', 'Yttrium', 'Atomic # = 39\nAtomic Weight = 88.91\nState = Solid\nCategory = Trans Metals'),
            ('La   >|', 'Lanthanum', 'Atomic # = 57\nAtomic Weight = 138.91\nState = Solid\nCategory = Trans Metals'),
            ('Ac   >|', 'Actinium', 'Atomic # = 89\nAtomic Weight = 227.03\nState = Solid\nCategory = Trans Metals')]
        r = 4
        c = 2
        for b in column3: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column4 = [
            ('Ti', 'Titanium', 'Atomic # = 22\nAtomic Weight = 47.90\nState = Solid\nCategory = Trans Metals'),
            ('Zr', 'Zirconium', 'Atomic # = 40\nAtomic Weight = 91.22\nState = Solid\nCategory = Trans Metals'),
            ('Hf', 'Hanium', 'Atomic # = 72\nAtomic Weight = 178.49\nState = Solid\nCategory = Trans Metals'),
            ('Rf', 'Rutherfordium', 'Atomic # = 104\nAtomic Weight = 261.00\nState = Synthetic\nCategory = Tran Metal')]
        r = 4
        c = 3
        for b in column4: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 10: 
                r = 1
                c += 1

        column5 = [
            ('V', 'Vanadium', 'Atomic # = 23\nAtomic Weight = 50.94\nState = Solid\nCategory = Trans Metals'),
            ('Nb', 'Niobium', 'Atomic # = 41\nAtomic Weight = 92.91\nState = Solid\nCategory = Trans Metals'),
            ('Ta', 'Tantalum', 'Atomic # = 73\nAtomic Weight = 180.95\nState = Solid\nCategory = Trans Metals'),
            ('Ha', 'Hahnium', 'Atomic # = 105\nAtomic Weight = 262.00\nState = Synthetic\nCategory = Trans Metals')]
        r = 4
        c = 4
        for b in column5: 
            tk.Button(self,
                      text=b[0],
                      width=5, height=2,
                      bg="light goldenrod",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 10: 
                r = 1
                c += 1

        column6 = [
            ('Cr', 'Chromium', 'Atomic # = 24\nAtomic Weight = 51.99\nState = Solid\nCategory = Trans Metals'),
            ('Mo', 'Molybdenum', 'Atomic # = 42\nAtomic Weight = 95.94\nState = Solid\nCategory = Trans Metals'),
            ('W', 'Tungsten', 'Atomic # = 74\nAtomic Weight = 183.85\nState = Solid\nCategory = Trans Metals'),
            ('Sg', 'Seaborgium', 'Atomic # = 106\nAtomic Weight = 266.00\nState = Synthetic\nCategory = Trans Metals')]
        r = 4
        c = 5
        for b in column6: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b:  self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column7 = [
            ('Mn', 'Manganese', 'Atomic # = 25\nAtomic Weight = 178.49\nState = Solid\nCategory = Trans Metals'),
            ('Tc', 'Technetium', 'Atomic # = 43\nAtomic Weight = 178.49\nState = Synthetic\nCategory = Trans Metals'),
            ('Re', 'Rhenium', 'Atomic # = 75\nAtomic Weight = 178.49\nState = Solid\nCategory = Trans Metals'),
            ('Bh', 'Bohrium', 'Atomic # = 107\nAtomic Weight = 262.00\nState = Synthetic\nCategory = Trans Metals')]
        r = 4
        c = 6
        for b in column7: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b:  self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column8 = [
            ('Fe', 'Iron', 'Atomic # = 26\nAtomic Weight = 55.85\nState = Solid\nCategory = Trans Metals'),
            ('Ru', 'Ruthenium', 'Atomic # = 44\nAtomic Weight = 101.07\nState = Solid\nCategory = Trans Metals'),
            ('Os', 'Osmium', 'Atomic # = 76\nAtomic Weight = 190.20\nState = Solid\nCategory = Trans Metals'),
            ('Hs', 'Hassium', 'Atomic # = 108\nAtomic Weight = 265.00\nState = Synthetic\nCategory = Trans Metals')]
        r = 4
        c = 7
        for b in column8: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column9 = [
            ('Co', 'Cobalt', 'Atomic # = 27\nAtomic Weight = 58.93\nState = Solid\nCategory = Trans Metals'),
            ('Rh', 'Rhodium', 'Atomic # = 45\nAtomic Weight = 102.91\nState = Solid\nCategory = Trans Metals'),
            ('Ir', 'Iridium', 'Atomic # = 77\nAtomic Weight = 192.22\nState = Solid\nCategory = Trans Metals'),
            ('Mt', 'Meitnerium', 'Atomic # = 109\nAtomic Weight = 266.00\nState = Synthetic\nCategory = Trans Metals')]
        r = 4
        c = 8
        for b in column9: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column10 = [
            ('Ni', 'Nickle', 'Atomic # = 28\nAtomic Weight = 58.70\nState = Solid\nCategory = Trans Metals'),
            ('Pd', 'Palladium', 'Atomic # = 46\nAtomic Weight = 106.40\nState = Solid\nCategory = Trans Metals'),
            ('Pt', 'Platinum', 'Atomic # = 78\nAtomic Weight = 195.09\nState = Solid\nCategory = Trans Metals')]
        r = 4
        c = 9
        for b in column10: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column11 = [
            ('Cu', 'Copper', 'Atomic # = 29\nAtomic Weight = 63.55\nState = Solid\nCategory = Trans Metals'),
            ('Ag', 'Silver', 'Atomic # = 47\nAtomic Weight = 107.97\nState = Solid\nCategory = Trans Metals'),
            ('Au', 'Gold', 'Atomic # = 79\nAtomic Weight = 196.97\nState = Solid\nCategory = Trans Metals')]
        r = 4
        c = 10
        for b in column11: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b:  self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column12 = [
            ('Zn', 'Zinc', 'Atomic # = 30\nAtomic Weight = 65.37\nState = Solid\nCategory = Trans Metals'),
            ('Cd', 'Cadmium', 'Atomic # = 48\nAtomic Weight = 112.41\nState = Solid\nCategory = Trans Metals'),
            ('Hg', 'Mercury', 'Atomic # = 80\nAtomic Weight = 200.59\nState = Liquid\nCategory = Trans Metals')]
        r = 4
        c = 11
        for b in column12: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b:  self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column13_1 = [
            ('B', 'Boron', 'Atomic # = 5\nAtomic Weight = 10.81\nState = Solid\nCategory = Nonmetals')]
        r = 2
        c = 12
        for b in column13_1: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Blue",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column13_2 = [
            ('Al', 'Aluminum', 'Atomic # = 13\nAtomic Weight = 26.98\nState = Solid\nCategory = Other Metals'),
            ('Ga', 'Gallium', 'Atomic # = 31\nAtomic Weight = 69.72\nState = Solid\nCategory = Other Metals'),
            ('In', 'Indium', 'Atomic # = 49\nAtomic Weight = 69.72\nState = Solid\nCategory = Other Metals'),
            ('Ti', 'Thallium', 'Atomic # = 81\nAtomic Weight = 204.37\nState = Solid\nCategory = Other Metals')]
        r = 3
        c = 12
        for b in column13_2: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Pink",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column14_1 = [
            ('C', 'Carbon', 'Atomic # = 6\nAtomic Weight = 12.01\nState = Solid\nCategory = Nonmetals'),
            ('Si', 'Silicon', 'Atomic # = 14\nAtomic Weight = 28.09\nState = Solid\nCategory = Nonmetals')]
        r = 2
        c = 13
        for b in column14_1: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Blue",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column14_2 = [
            ('Ge', 'Germanium', 'Atomic # = 32\nAtomic Weight = 72.59\nState = Solid\nCategory = Other Metals'),
            ('Sn', 'Tin', 'Atomic # = 50\nAtomic Weight = 118.69\nState = Solid\nCategory = Other Metals'),
            ('Pb', 'Lead', 'Atomic # = 82\nAtomic Weight = 207.20\nState = Solid\nCategory = Other Metals')]
        r = 4
        c = 13
        for b in column14_2: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Pink",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column15_1 = [
            ('N', 'Nitrogen', 'Atomic # = 7\nAtomic Weight = 14.01\nState = Gas\nCategory = Nonmetals'),
            ('P', 'Phosphorus', 'Atomic # = 15\nAtomic Weight = 30.97\nState = Solid\nCategory = Nonmetals'),
            ('As', 'Arsenic', 'Atomic # = 33\nAtomic Weight = 74.92\nState = Solid\nCategory = Nonmetals')]
        r = 2
        c = 14
        for b in column15_1: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Blue",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column15_2 = [
            ('Sb', 'Antimony', 'Atomic # = 51\nAtomic Weight = 121.75\nState = Solid\nCategory = Other Metals'),
            ('Bi', 'Bismuth', 'Atomic # = 83\nAtomic Weight = 208.98\nState = Solid\nCategory = Other Metals')]
        r = 5
        c = 14
        for b in column15_2: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Pink",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column16_1 = [
            ('O', 'Oxygen', 'Atomic # = 8\nAtomic Weight = 15.99\nState = Gas\nCategory = Nonmetals'),
            ('S', 'Sulfur', 'Atomic # = 16\nAtomic Weight = 32.06\nState = Solid\nCategory = Nonmetals'),
            ('Se', 'Selenium', 'Atomic # = 34\nAtomic Weight = 78.96\nState = Solid\nCategory = Nonmetals'),
            ('Te', 'Tellurium', 'Atomic # = 52\nAtomic Weight = 127.60\nState = Solid\nCategory = Nonmetals')]
        r = 2
        c = 15
        for b in column16_1: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Blue",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column16_2 = [
            ('Po', 'Polonium', 'Atomic # = 84\nAtomic Weight = 209.00\nState = Solid\nCategory = Other Metals')]
        r = 6
        c = 15
        for b in column16_2: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Pink",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column17 = [
            ('F', 'Fluorine', 'Atomic # = 9\nAtomic Weight = 18.99\nState = Gas\nCategory = Nonmetals'),
            ('Cl', 'Chlorine', 'Atomic # = 17\nAtomic Weight = 35.45\nState = Gas\nCategory = Nonmetals'),
            ('Br', 'Bromine', 'Atomic # = 35\nAtomic Weight = 79.90\nState = Liquid\nCategory = Nonmetals'),
            ('I', 'Iodine', 'Atomic # = 53\nAtomic Weight = 126.90\nState = Solid\nCategory = Nonmetals'),
            ('At', 'Astatine', 'Atomic # = 85\nAtomic Weight = 210.00\nState = Solid\nCategory = Nonmetals')]
        r = 2
        c = 16
        for b in column17: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="Light Blue",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        column18 = [
            ('He', 'Helium', 'Atomic # = 2\nAtomic Weight = 4.00\nState = Gas\nCategory = Nobel Gases'),
            ('Ne', 'Neon', 'Atomic # = 10\nAtomic Weight = 20.18\nState = Gas\nCategory = Nobel Gases'),
            ('Ar', 'Argon', 'Atomic # = 18\nAtomic Weight = 39.95\nState = Gas\nCategory = Nobel Gases'),
            ('Kr', 'Krypton', 'Atomic # = 36\nAtomic Weight = 83.80\nState = Gas\nCategory = Nobel Gases'),
            ('Xe', 'Xenon', 'Atomic # = 54\nAtomic Weight = 131.30\nState = Gas\nCategory = Nobel Gases'),
            ('Rn', 'Radon', 'Atomic # = 86\nAtomic Weight = 222.00\nState = Gas\nCategory = Nobel Gases')]
        r = 1
        c = 17
        for b in column18: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="indian red",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        self.fillerLine = tk.Label(self, text="")
        self.fillerLine.grid(row=10, column=0)

        lanthanide = [
            ('>| Ce', 'Cerium', 'Atomic # = 58\nAtomic Weight = 140.12\nState = Solid\nCategory = Trans Metals'),
            ('Pr', 'Praseodymium', 'Atomic # = 59\nAtomic Weight = 140.91\nState = Solid\nCategory = Trans Metals'),
            ('Nd', 'Neodymium', 'Atomic # = 60\nAtomic Weight = 144.24\nState = Solid\nCategory = Trans Metals'),
            ('Pm', 'Promethium', 'Atomic # = 61\nAtomic Weight = 145.00\nState = Synthetic\nCategory = Trans Metals'),
            ('Sm', 'Samarium', 'Atomic # = 62\nAtomic Weight = 150.40\nState = Solid\nCategory = Trans Metals'),
            ('Eu', 'Europium', 'Atomic # = 63\nAtomic Weight = 151.96\nState = Solid\nCategory = Trans Metals'),
            ('Gd', 'Gadolinium', 'Atomic # = 64\nAtomic Weight = 157.25\nState = Solid\nCategory = Trans Metals'),
            ('Tb', 'Terbium', 'Atomic # = 65\nAtomic Weight = 158.93\nState = Solid\nCategory = Trans Metals'),
            ('Dy', 'Dyprosium', 'Atomic # = 66\nAtomic Weight = 162.50\nState = Solid\nCategory = Trans Metals'),
            ('Ho', 'Holmium', 'Atomic # = 67\nAtomic Weight = 164.93\nState = Solid\nCategory = Trans Metals'),
            ('Er', 'Erbium', 'Atomic # = 68\nAtomic Weight = 167.26\nState = Solid\nCategory = Trans Metals'),
            ('Tm', 'Thulium', 'Atomic # = 69\nAtomic Weight = 168.93\nState = Solid\nCategory = Trans Metals'),
            ('Yb', 'Ytterbium', 'Atomic # = 70\nAtomic Weight = 173.04\nState = Solid\nCategory = Trans Metals'),
            ('Lu', 'Lutetium', 'Atomic # = 71\nAtomic Weight = 174.97\nState = Solid\nCategory = Trans Metals')]
        r = 11
        c = 3
        for b in lanthanide: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            c += 1
            if c > 18: 
                c = 1
                r += 1

        actinide = [
            ('>| Th', 'Thorium', 'Atomic # = 90\nAtomic Weight = 232.04\nState = Solid\nCategory = Trans Metals'),
            ('Pa', 'Protactinium', 'Atomic # = 91\nAtomic Weight = 231.04\nState = Solid\nCategory = Trans Metals'),
            ('U', 'Uranium', 'Atomic # = 92\nAtomic Weight = 238.03\nState = Solid\nCategory = Trans Metals'),
            ('Np', 'Neptunium', 'Atomic # = 93\nAtomic Weight = 237.05\nState = Synthetic\nCategory = Trans Metals'),
            ('Pu', 'Plutonium', 'Atomic # = 94\nAtomic Weight = 244.00\nState = Synthetic\nCategory = Trans Metals'),
            ('Am', 'Americium', 'Atomic # = 95\nAtomic Weight = 243.00\nState = Synthetic\nCategory = Trans Metals'),
            ('Cm', 'Curium', 'Atomic # = 96\nAtomic Weight = 247\nState = Synthetic\nCategory = Trans Metals'),
            ('Bk', 'Berkelium', 'Atomic # = 97\nAtomic Weight = 247\nState = Synthetic\nCategory = Trans Metals'),
            ('Cf', 'Californium', 'Atomic # = 98\nAtomic Weight = 247\nState = Synthetic\nCategory = Trans Metals'),
            ('Es', 'Einsteinium', 'Atomic # = 99\nAtomic Weight = 252.00\nState = Synthetic\nCategory = Trans Metals'),
            ('Fm', 'Fermium', 'Atomic # = 100\nAtomic Weight = 257.00\nState = Synthetic\nCategory = Trans Metals'),
            ('Md', 'Mendelevium', 'Atomic # = 101\nAtomic Weight = 260.00\nState = Synthetic\nCategory = Trans Metals'),
            ('No', 'Nobelium', 'Atomic # = 102\nAtomic Weight = 259\nState = Synthetic\nCategory = Trans Metals'),
            ('Lr', 'Lawrencium', 'Atomic # = 103\nAtomic Weight = 262\nState = Synthetic\nCategory = Trans Metals')]
        r = 12
        c = 3
        for b in actinide:
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg="light goldenrod",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            c += 1
            if c > 18: 
                c = 1
                r += 1

        reset = [
            ('Reset', 'Click the element you would like information about.', '')]
        r = 12
        c = 0
        for b in reset: 
            tk.Button(self,
                      text=b[0],
                      width=5,
                      height=2,
                      bg= "black",
                      fg = "white",
                      command=lambda text=b: self.name(text[1]) & self.info(text[2])).grid(row=r, column=c)
            r += 1
            if r > 7: 
                r = 1
                c += 1

        self.infoLine = tk.Label(self, text="", justify='left')
        self.infoLine.grid(row=1, column=3, columnspan=10, rowspan=4)

    


    # Replaces Label at the top with the name of whichever element tk.Button was pressed
    def name(self, text): 
        self.topLabel.config(text=text)

    # Displays information on the element of whichever element tk.Button was pressed
    def info(self, text): 
        self.infoLine.config(text=text)



root = tk.Tk()
a = App(root)
a.grid(row=0, column=0, sticky='nsew') 
a.mainloop()
```
New contributor
mmx is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
2
  • 1
    \$\begingroup\$ A great project! Please correct alias for Thallium (81) in column13_2: 'Ti' -> 'Tl' \$\endgroup\$
    – viktory
    yesterday
  • 1
    \$\begingroup\$ This is now one of three similar posts making a period table in tkinter. I encourage you to read the other two to get ideas. \$\endgroup\$
    – Reinderien
    yesterday
2
\$\begingroup\$

Cool project!

I would definitely bring all of the hard-coded information about the elements to the top of the module, this will make your code much more readable as well as enabling you to make it shorter as I will show.

Personally I would probably use a dictionary where the columns are the keys. And since the rows that are used vary from column to column, you'll need that information in there too so the values of the dictionary could be lists of (row, element) pairs like this:

elements_by_column = {
    1: [
        (1, ('H', 'Hydrogen', 'Atomic # = 1\nAtomic Weight =1.01\nState = Gas\nCategory = Alkali Metals')),
        (2, ('Li', 'Lithium', 'Atomic # = 3\nAtomic Weight = 6.94\nState = Solid\nCategory = Alkali Metals')),
        (3, ('Na', 'Sodium', 'Atomic # = 11\nAtomic Weight = 22.99\nState = Solid\nCategory = Alkali Metals')),
        ...
    ],
    ...
}

As far as I can tell, the only other thing that varies from column to column is the background colour, so you can define that at the beginning too:

column_colors = {
    1: "grey",
    2: "light goldenrod",
    ...
}

With this done, you should be able to replace the code for making the buttons with something like the following:

num_cols = len(elements_by_column.keys())

for col in range(1, num_cols + 1):
    for row, element in elements_by_column[col]:
        tk.Button(self,
                text=element[0],
                width=5,
                height=2,
                bg=column_colors[col],
                command=lambda text=element:  self.name(text[1]) & self.info(text[2])).grid(row=row, column=col)

I haven't tested this out so there may be some slight adjustments to be made but it should be equivalent to what you have at the moment. I hope that helps!

\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot! This is very helpful \$\endgroup\$
    – mmx
    2 days ago
1
\$\begingroup\$

(Edited) Cool project, how about this approach:

  1. Move elements data to a json - This answers:

I wanted to make this code shorter ...

By moving elements information to a separate file you make main file much smaller by separating concerns between Data and Business logic. Also this allows you to easily reuse elements data for other projects. (Please see elements.json below)

  1. Make a layout and color maps for buttons - This answers:

Is there a simpler, ... way of generating the buttons in this code?

You create a layout map for each element button in a separate file, and use only one method build_elements_table() to generate the whole table of elements. (Please see elements_layout_map.json below)

  1. This code executes but also throws exception at the end:
 command=lambda text=b: self.name(text[1]) & self.info(text[2])

if you want to execute several methods in this lambda expression: use comma separator instead of "&" and put method calls in braces like this:

 command=lambda text=b: (self.name(text[1]), self.info(text[2]))
  1. Regarding selecting buttons by arrows (not implemented in code below) : 4.1 add event handlers for each arrow press 4.2 create button_layout_map with references on each button 4.3 track current selected button row/column indexes in button_layout_map

P.S. (please create four files from the code below and put them in one folder)

periodic_table.py

import tkinter as tk

class PeriodicTable(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        '''Initializes a new instance of the PeriodicTable class'''

        # strings
        # TODO: move strings to separate localizable dictionary
        self.default_title = "Periodic Table of the Elements"
        self.default_header = "Click the element you would like information about."
        self.reset_button_text = "Reset"

        # get data for building the elements table
        self.elements_data = self.get_elements_data()
        self.elements_map = self.get_elements_map()
        self.elements_color_map = self.get_elements_color_map()

        # add controls on UI
        self.buttons = {}
        self.parent = parent
        self.winfo_toplevel().title(self.default_title)
        self.topLabel = tk.Label(self, text=self.default_header, font=20)
        self.topLabel.grid(row=0, column=0, columnspan=18)
        self.build_elements_table()
        self.infoLine = tk.Label(self, text="", justify='left')
        self.infoLine.grid(row=1, column=3, columnspan=10, rowspan=4)
        self.reset_button = tk.Button(self, text=self.reset_button_text, width=5, height=2, bg= "black", fg = "white",
                                      command=lambda text="": (self.change_header(self.default_header), self.change_info("")))
        self.reset_button.grid(row=12, column=0)

    def get_elements_data(self):
        '''Loads elements data from json'''

        import json
        with open('elements.json', 'r') as file:
            return json.loads(file.read())

    def get_elements_map(self):
        '''Loads elements layout map from json'''

        import json
        with open('elements_layout_map.json', 'r') as file:
            return json.loads(file.read())

    def get_elements_color_map(self):
        '''Load elements map from json'''

        import json
        with open('color_map.json', 'r') as file:
            return json.loads(file.read())

    def build_elements_table(self):
        '''Adds button controls for each element on UI'''

        for element in self.elements_data["elements"]:

            name = element["Alias"]
            row = self.elements_map["map"][element["Alias"]]["row"]
            col = self.elements_map["map"][element["Alias"]]["column"]
            color = self.elements_color_map['map'][element["Categories"][0]]

            def get_info(element):
                return "Atomic # = {}\nAtomic Weight = {}\nState = {}\nCategory = {}".format(
                       element['Atomic Number'],
                       element['Atomic Weight'],
                       element['State'],
                       element['Categories'][0])

            button = tk.Button(self, text=element["Alias"], width=5, height=2, bg=color,
                               command=lambda  e=element: (self.change_header(e["Name"]), self.change_info(get_info(e))))
            
            button.grid(row=row, column=col)            
            self.buttons[element["Alias"]] = button

        self.fillerLine = tk.Label(self, text="")
        self.fillerLine.grid(row=10, column=0)

    def change_header(self, text): 
        '''Changes Label text at the top with the name of whichever 
        element tk.Button was pressed'''

        self.topLabel.config(text=text)

    def change_info(self, text):
        '''Displays information on the element of 
        whichever element tk.Button was pressed'''

        self.infoLine.config(text=text)

root = tk.Tk()
app = PeriodicTable(root)
app.grid(row=0, column=0, sticky='nsew') 
root.resizable(width=False, height=False)
app.mainloop()

elements.json

{
    "version": 1,
    "elements": [
        {
            "Alias": "H",
            "Name": "Hydrogen",
            "Atomic Number": 1,
            "Atomic Weight": 1.01,
            "State": "Gas",
            "Categories": ["Alkali Metals"]
        },
        {
            "Alias": "Li",
            "Name": "Lithium",
            "Atomic Number": 3,
            "Atomic Weight": 6.94,
            "State": "Solid",
            "Categories": ["Alkali Metals"]
        },
        {
            "Alias": "Na",
            "Name": "Sodium",
            "Atomic Number": 11,
            "Atomic Weight": 22.99,
            "State": "Solid",
            "Categories": ["Alkali Metals"]
        },
        {
            "Alias": "K",
            "Name": "Potassium",
            "Atomic Number": 19,
            "Atomic Weight": 39.10,
            "State": "Solid",
            "Categories": ["Alkali Metals"]
        },
        {
            "Alias": "Rb",
            "Name": "Rubidium",
            "Atomic Number": 37,
            "Atomic Weight": 85.47,
            "State": "Solid",
            "Categories": ["Alkali Metals"]
        },
        {
            "Alias": "Cs",
            "Name": "Cesium",
            "Atomic Number": 55,
            "Atomic Weight": 132.91,
            "State": "Solid",
            "Categories": ["Alkali Metals"]
        },
        {
            "Alias": "Fr",
            "Name": "Francium",
            "Atomic Number": 87,
            "Atomic Weight": 223.00,
            "State": "Solid",
            "Categories": ["Alkali Metals"]
        },
        {
            "Alias": "Be",
            "Name": "Beryllium",
            "Atomic Number": 4,
            "Atomic Weight": 9.01,
            "State": "Solid",
            "Categories": ["Alkaline Earth Metals"]
        },
        {
            "Alias": "Mg",
            "Name": "Magnesium",
            "Atomic Number": 12,
            "Atomic Weight": 24.31,
            "State": "Solid",
            "Categories": ["Alkaline Earth Metals"]
        },
        {
            "Alias": "Ca",
            "Name": "Calcium",
            "Atomic Number": 20,
            "Atomic Weight": 40.08,
            "State": "Solid",
            "Categories": ["Alkaline Earth Metals"]
        },
        {
            "Alias": "Sr",
            "Name": "Strontium",
            "Atomic Number": 38,
            "Atomic Weight": 87.62,
            "State": "Solid",
            "Categories": ["Alkaline Earth Metals"]
        },
        {
            "Alias": "Ba",
            "Name": "Barium",
            "Atomic Number": 56,
            "Atomic Weight": 137.33,
            "State": "Solid",
            "Categories": ["Alkaline Earth Metals"]
        },
        {
            "Alias": "Ra",
            "Name": "Radium",
            "Atomic Number": 88,
            "Atomic Weight": 226.03,
            "State": "Solid",
            "Categories": ["Alkaline Earth Metals"]
        },
        {
            "Alias": "Sc",
            "Name": "Scandium",
            "Atomic Number": 21,
            "Atomic Weight": 44.96,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Y",
            "Name": "Yttrium",
            "Atomic Number": 39,
            "Atomic Weight": 88.91,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "La   >|",
            "Name": "Lanthanum",
            "Atomic Number": 57,
            "Atomic Weight": 138.91,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Ac   >|",
            "Name": "Actinium",
            "Atomic Number": 89,
            "Atomic Weight": 227.03,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Ti",
            "Name": "Titanium",
            "Atomic Number": 22,
            "Atomic Weight": 47.90,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Zr",
            "Name": "Zirconium",
            "Atomic Number": 40,
            "Atomic Weight": 91.22,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Hf",
            "Name": "Hanium",
            "Atomic Number": 72,
            "Atomic Weight": 178.49,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Rf",
            "Name": "Rutherfordium",
            "Atomic Number": 104,
            "Atomic Weight": 261.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "V",
            "Name": "Vanadium",
            "Atomic Number": 23,
            "Atomic Weight": 50.94,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Nb",
            "Name": "Niobium",
            "Atomic Number": 41,
            "Atomic Weight": 92.91,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Ta",
            "Name": "Tantalum",
            "Atomic Number": 73,
            "Atomic Weight": 180.95,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Ha",
            "Name": "Hahnium",
            "Atomic Number": 105,
            "Atomic Weight": 262.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Cr",
            "Name": "Chromium",
            "Atomic Number": 24,
            "Atomic Weight": 51.99,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Mo",
            "Name": "Molybdenum",
            "Atomic Number": 42,
            "Atomic Weight": 95.94,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "W",
            "Name": "Tungsten",
            "Atomic Number": 74,
            "Atomic Weight": 183.85,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Sg",
            "Name": "Seaborgium",
            "Atomic Number": 106,
            "Atomic Weight": 266.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Mn",
            "Name": "Manganese",
            "Atomic Number": 25,
            "Atomic Weight": 178.49,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Tc",
            "Name": "Technetium",
            "Atomic Number": 43,
            "Atomic Weight": 178.49,
            "State": "Synthetic",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Re",
            "Name": "Rhenium",
            "Atomic Number": 75,
            "Atomic Weight": 178.49,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Bh",
            "Name": "Bohrium",
            "Atomic Number": 107,
            "Atomic Weight": 262.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Fe",
            "Name": "Iron",
            "Atomic Number": 26,
            "Atomic Weight": 55.85,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Ru",
            "Name": "Ruthenium",
            "Atomic Number": 44,
            "Atomic Weight": 101.07,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Os",
            "Name": "Osmium",
            "Atomic Number": 76,
            "Atomic Weight": 190.20,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Hs",
            "Name": "Hassium",
            "Atomic Number": 108,
            "Atomic Weight": 265.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Co",
            "Name": "Cobalt",
            "Atomic Number": 27,
            "Atomic Weight": 58.93,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Rh",
            "Name": "Rhodium",
            "Atomic Number": 45,
            "Atomic Weight": 102.91,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Ir",
            "Name": "Iridium",
            "Atomic Number": 77,
            "Atomic Weight": 192.22,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Mt",
            "Name": "Meitnerium",
            "Atomic Number": 109,
            "Atomic Weight": 266.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Ni",
            "Name": "Nickle",
            "Atomic Number": 28,
            "Atomic Weight": 58.70,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Pd",
            "Name": "Palladium",
            "Atomic Number": 46,
            "Atomic Weight": 106.40,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Pt",
            "Name": "Platinum",
            "Atomic Number": 78,
            "Atomic Weight": 195.09,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Cu",
            "Name": "Copper",
            "Atomic Number": 29,
            "Atomic Weight": 63.55,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Ag",
            "Name": "Silver",
            "Atomic Number": 47,
            "Atomic Weight": 107.97,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Au",
            "Name": "Gold",
            "Atomic Number": 79,
            "Atomic Weight": 196.97,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Zn",
            "Name": "Zinc",
            "Atomic Number": 30,
            "Atomic Weight": 65.37,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Cd",
            "Name": "Cadmium",
            "Atomic Number": 48,
            "Atomic Weight": 112.41,
            "State": "Solid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "Hg",
            "Name": "Mercury",
            "Atomic Number": 80,
            "Atomic Weight": 200.59,
            "State": "Liquid",
            "Categories": ["Trans Metals"]
        },
        {
            "Alias": "B",
            "Name": "Boron",
            "Atomic Number": 5,
            "Atomic Weight": 10.81,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Al",
            "Name": "Aluminum",
            "Atomic Number": 13,
            "Atomic Weight": 26.98,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "Ga",
            "Name": "Gallium",
            "Atomic Number": 31,
            "Atomic Weight": 69.72,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "In",
            "Name": "Indium",
            "Atomic Number": 49,
            "Atomic Weight": 69.72,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "Tl",
            "Name": "Thallium",
            "Atomic Number": 81,
            "Atomic Weight": 204.37,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "C",
            "Name": "Carbon",
            "Atomic Number": 6,
            "Atomic Weight": 12.01,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Si",
            "Name": "Silicon",
            "Atomic Number": 14,
            "Atomic Weight": 28.09,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Ge",
            "Name": "Germanium",
            "Atomic Number": 32,
            "Atomic Weight": 72.59,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "Sn",
            "Name": "Tin",
            "Atomic Number": 50,
            "Atomic Weight": 118.69,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "Pb",
            "Name": "Lead",
            "Atomic Number": 82,
            "Atomic Weight": 207.20,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "N",
            "Name": "Nitrogen",
            "Atomic Number": 7,
            "Atomic Weight": 14.01,
            "State": "Gas",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "P",
            "Name": "Phosphorus",
            "Atomic Number": 15,
            "Atomic Weight": 30.97,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "As",
            "Name": "Arsenic",
            "Atomic Number": 33,
            "Atomic Weight": 74.92,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Sb",
            "Name": "Antimony",
            "Atomic Number": 51,
            "Atomic Weight": 121.75,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "Bi",
            "Name": "Bismuth",
            "Atomic Number": 83,
            "Atomic Weight": 208.98,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "O",
            "Name": "Oxygen",
            "Atomic Number": 8,
            "Atomic Weight": 15.99,
            "State": "Gas",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "S",
            "Name": "Sulfur",
            "Atomic Number": 16,
            "Atomic Weight": 32.06,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Se",
            "Name": "Selenium",
            "Atomic Number": 34,
            "Atomic Weight": 78.96,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Te",
            "Name": "Tellurium",
            "Atomic Number": 52,
            "Atomic Weight": 127.60,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Po",
            "Name": "Polonium",
            "Atomic Number": 84,
            "Atomic Weight": 209.00,
            "State": "Solid",
            "Categories": ["Other Metals"]
        },
        {
            "Alias": "F",
            "Name": "Fluorine",
            "Atomic Number": 9,
            "Atomic Weight": 18.99,
            "State": "Gas",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Cl",
            "Name": "Chlorine",
            "Atomic Number": 17,
            "Atomic Weight": 35.45,
            "State": "Gas",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "Br",
            "Name": "Bromine",
            "Atomic Number": 35,
            "Atomic Weight": 79.90,
            "State": "Liquid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "I",
            "Name": "Iodine",
            "Atomic Number": 53,
            "Atomic Weight": 126.90,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "At",
            "Name": "Astatine",
            "Atomic Number": 85,
            "Atomic Weight": 210.00,
            "State": "Solid",
            "Categories": ["Nonmetals"]
        },
        {
            "Alias": "He",
            "Name": "Helium",
            "Atomic Number": 2,
            "Atomic Weight": 4.00,
            "State": "Gas",
            "Categories": ["Nobel Gases"]
        },
        {
            "Alias": "Ne",
            "Name": "Neon",
            "Atomic Number": 10,
            "Atomic Weight": 20.18,
            "State": "Gas",
            "Categories": ["Nobel Gases"]
        },
        {
            "Alias": "Ar",
            "Name": "Argon",
            "Atomic Number": 18,
            "Atomic Weight": 39.95,
            "State": "Gas",
            "Categories": ["Nobel Gases"]
        },
        {
            "Alias": "Kr",
            "Name": "Krypton",
            "Atomic Number": 36,
            "Atomic Weight": 83.80,
            "State": "Gas",
            "Categories": ["Nobel Gases"]
        },
        {
            "Alias": "Xe",
            "Name": "Xenon",
            "Atomic Number": 54,
            "Atomic Weight": 131.30,
            "State": "Gas",
            "Categories": ["Nobel Gases"]
        },
        {
            "Alias": "Rn",
            "Name": "Radon",
            "Atomic Number": 86,
            "Atomic Weight": 222.00,
            "State": "Gas",
            "Categories": ["Nobel Gases"]
        },
        {
            "Alias": ">| Ce",
            "Name": "Cerium",
            "Atomic Number": 58,
            "Atomic Weight": 140.12,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Pr",
            "Name": "Praseodymium",
            "Atomic Number": 59,
            "Atomic Weight": 140.91,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Nd",
            "Name": "Neodymium",
            "Atomic Number": 60,
            "Atomic Weight": 144.24,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Pm",
            "Name": "Promethium",
            "Atomic Number": 61,
            "Atomic Weight": 145.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Sm",
            "Name": "Samarium",
            "Atomic Number": 62,
            "Atomic Weight": 150.40,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Eu",
            "Name": "Europium",
            "Atomic Number": 63,
            "Atomic Weight": 151.96,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Gd",
            "Name": "Gadolinium",
            "Atomic Number": 64,
            "Atomic Weight": 157.25,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Tb",
            "Name": "Terbium",
            "Atomic Number": 65,
            "Atomic Weight": 158.93,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Dy",
            "Name": "Dyprosium",
            "Atomic Number": 66,
            "Atomic Weight": 162.50,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Ho",
            "Name": "Holmium",
            "Atomic Number": 67,
            "Atomic Weight": 164.93,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Er",
            "Name": "Erbium",
            "Atomic Number": 68,
            "Atomic Weight": 167.26,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Tm",
            "Name": "Thulium",
            "Atomic Number": 69,
            "Atomic Weight": 168.93,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Yb",
            "Name": "Ytterbium",
            "Atomic Number": 70,
            "Atomic Weight": 173.04,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": "Lu",
            "Name": "Lutetium",
            "Atomic Number": 71,
            "Atomic Weight": 174.97,
            "State": "Solid",
            "Categories": ["Trans Metals", "Lanthanides"]
        },
        {
            "Alias": ">| Th",
            "Name": "Thorium",
            "Atomic Number": 90,
            "Atomic Weight": 232.04,
            "State": "Solid",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Pa",
            "Name": "Protactinium",
            "Atomic Number": 91,
            "Atomic Weight": 231.04,
            "State": "Solid",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "U",
            "Name": "Uranium",
            "Atomic Number": 92,
            "Atomic Weight": 238.03,
            "State": "Solid",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Np",
            "Name": "Neptunium",
            "Atomic Number": 93,
            "Atomic Weight": 237.05,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Pu",
            "Name": "Plutonium",
            "Atomic Number": 94,
            "Atomic Weight": 244.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Am",
            "Name": "Americium",
            "Atomic Number": 95,
            "Atomic Weight": 243.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Cm",
            "Name": "Curium",
            "Atomic Number": 96,
            "Atomic Weight": 247,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Bk",
            "Name": "Berkelium",
            "Atomic Number": 97,
            "Atomic Weight": 247,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Cf",
            "Name": "Californium",
            "Atomic Number": 98,
            "Atomic Weight": 247,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Es",
            "Name": "Einsteinium",
            "Atomic Number": 99,
            "Atomic Weight": 252.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Fm",
            "Name": "Fermium",
            "Atomic Number": 100,
            "Atomic Weight": 257.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Md",
            "Name": "Mendelevium",
            "Atomic Number": 101,
            "Atomic Weight": 260.00,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "No",
            "Name": "Nobelium",
            "Atomic Number": 102,
            "Atomic Weight": 259,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        },
        {
            "Alias": "Lr",
            "Name": "Lawrencium",
            "Atomic Number": 103,
            "Atomic Weight": 262,
            "State": "Synthetic",
            "Categories": ["Trans Metals", "Actinides"]
        }
    ]
}

elements_layout_map.json

{
    "version": 1,
    "map": {
        "H": { "row": 1, "column" : 0 },
        "Li": { "row": 2, "column" : 0 },
        "Na": { "row": 3, "column": 0 },
        "K": { "row": 4, "column": 0 },
        "Rb": { "row": 5, "column": 0 },
        "Cs": { "row": 6, "column": 0 },
        "Fr": { "row": 7, "column": 0 },
        "Be": { "row": 2, "column": 1 },
        "Mg": { "row": 3, "column": 1 },
        "Ca": { "row": 4, "column": 1 },
        "Sr": { "row": 5, "column": 1 },
        "Ba": { "row": 6, "column": 1 },
        "Ra": { "row": 7, "column": 1 },
        "Sc": { "row": 4, "column": 2 },
        "Y": { "row": 5, "column": 2 },
        "La   >|": { "row": 6, "column": 2 },
        "Ac   >|": { "row": 7, "column": 2 },
        "Ti": { "row": 4, "column": 3 },
        "Zr": { "row": 5, "column": 3 },
        "Hf": { "row": 6, "column": 3 },
        "Rf": { "row": 7, "column": 3 },
        "V": { "row": 4, "column": 4 },
        "Nb": { "row": 5, "column": 4 },
        "Ta": { "row": 6, "column": 4 },
        "Ha": { "row": 7, "column": 4 },
        "Cr": { "row": 4, "column": 5 },
        "Mo": { "row": 5, "column": 5 },
        "W": { "row": 6, "column": 5 },
        "Sg": { "row": 7, "column": 5 },
        "Mn": { "row": 4, "column": 6 },
        "Tc": { "row": 5, "column": 6 },
        "Re": { "row": 6, "column": 6 },
        "Bh": { "row": 7, "column": 6 },
        "Fe": { "row": 4, "column": 7 },
        "Ru": { "row": 5, "column": 7 },
        "Os": { "row": 6, "column": 7 },
        "Hs": { "row": 7, "column": 7 },
        "Co": { "row": 4, "column": 8 },
        "Rh": { "row": 5, "column": 8 },
        "Ir": { "row": 6, "column": 8 },
        "Mt": { "row": 7, "column": 8 },
        "Ni": { "row": 4, "column": 9 },
        "Pd": { "row": 5, "column": 9 },
        "Pt": { "row": 6, "column": 9 },
        "Cu": { "row": 4, "column": 10 },
        "Ag": { "row": 5, "column": 10 },
        "Au": { "row": 6, "column": 10 },
        "Zn": { "row": 4, "column": 11 },
        "Cd": { "row": 5, "column": 11 },
        "Hg": { "row": 6, "column": 11 },
        "B": { "row": 2, "column": 12 },
        "Al": { "row": 3, "column": 12 },
        "Ga": { "row": 4, "column": 12 },
        "In": { "row": 5, "column": 12 },
        "Tl": { "row": 6, "column": 12 },
        "C": { "row": 2, "column": 13 },
        "Si": { "row": 3, "column": 13 },
        "Ge": { "row": 4, "column": 13 },
        "Sn": { "row": 5, "column": 13 },
        "Pb": { "row": 6, "column": 13 },
        "N": { "row": 2, "column": 14 },
        "P": { "row": 3, "column": 14 },
        "As": { "row": 4, "column": 14 },
        "Sb": { "row": 5, "column": 14 },
        "Bi": { "row": 6, "column": 14 },
        "O": { "row": 2, "column": 15 },
        "S": { "row": 3, "column": 15 },
        "Se": { "row": 4, "column": 15 },
        "Te": { "row": 5, "column": 15 },
        "Po": { "row": 6, "column": 15 },
        "F": { "row": 2, "column": 16 },
        "Cl": { "row": 3, "column": 16 },
        "Br": { "row": 4, "column": 16 },
        "I": { "row": 5, "column": 16 },
        "At": { "row": 6, "column": 16 },
        "He": { "row": 1, "column": 17 },
        "Ne": { "row": 2, "column": 17 },
        "Ar": { "row": 3, "column": 17 },
        "Kr": { "row": 4, "column": 17 },
        "Xe": { "row": 5, "column": 17 },
        "Rn": { "row": 6, "column": 17 },
        ">| Ce": { "row": 11, "column": 3 },
        "Pr": { "row": 11, "column": 4 },
        "Nd": { "row": 11, "column": 5 },
        "Pm": { "row": 11, "column": 6 },
        "Sm": { "row": 11, "column": 7 },
        "Eu": { "row": 11, "column": 8 },
        "Gd": { "row": 11, "column": 9 },
        "Tb": { "row": 11, "column": 10 },
        "Dy": { "row": 11, "column": 11 },
        "Ho": { "row": 11, "column": 12 },
        "Er": { "row": 11, "column": 13 },
        "Tm": { "row": 11, "column": 14 },
        "Yb": { "row": 11, "column": 15 },
        "Lu": { "row": 11, "column": 16 },
        ">| Th": { "row": 12, "column": 3 },
        "Pa": { "row": 12, "column": 4 },
        "U" : { "row": 12, "column": 5 },   
        "Np": { "row": 12, "column": 6 },   
        "Pu": { "row": 12, "column": 7 },   
        "Am": { "row": 12, "column": 8 },   
        "Cm": { "row": 12, "column": 9 },   
        "Bk": { "row": 12, "column": 10 },   
        "Cf": { "row": 12, "column": 11 },   
        "Es": { "row": 12, "column": 12 },   
        "Fm": { "row": 12, "column": 13 },   
        "Md": { "row": 12, "column": 14 },   
        "No": { "row": 12, "column": 15 },   
        "Lr": { "row": 12, "column": 16 }  
    }
}

color_map.json

{
    "version": 1,
    "map": {
        "Alkali Metals": "grey",
        "Alkaline Earth Metals": "light green",
        "Trans Metals": "light goldenrod",
        "Nonmetals": "Light Blue",
        "Other Metals": "Light Pink",
        "Nobel Gases": "indian red"
    }
}
New contributor
viktory is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer. \$\endgroup\$ yesterday
  • \$\begingroup\$ Thanks a lot! It is much neater and better version \$\endgroup\$
    – mmx
    yesterday
  • \$\begingroup\$ @TobySpeight, thanks for the remark. I've updated my post. \$\endgroup\$
    – viktory
    58 mins ago

Your Answer

mmx is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.