简体   繁体   中英

How to change text spacing for text in a Tkinter Label?

I have a Label that needs to displaying the following information:

    json = {
        "H3": {
            "atom": "H3",
            "scheme": "NH3_ISA-GRID",
            "type": "HN",
            "rank": "4",
            "moments": [
                "          Q0        Q1        Q2        Q3        Q4",
                "0   0.353619 -0.000000  0.022593 -0.000000  0.016054",
                "1s       NaN -0.020984 -0.000000 -0.010761 -0.000000",
                "1c       NaN -0.009221 -0.000000  0.007970 -0.000000",
                "2s       NaN       NaN -0.016711 -0.000000  0.015248",
                "2c       NaN       NaN  0.016692 -0.000000 -0.009410",
                "3s       NaN       NaN       NaN  0.003688 -0.000001",
                "3c       NaN       NaN       NaN  0.025270 -0.000001",
                "4s       NaN       NaN       NaN       NaN  0.005240",
                "4c       NaN       NaN       NaN       NaN  0.010030"
            ],
            "file": "/Users/gianluca/Desktop/project/example_molecules/ISA/OUT/NH3_ISA-GRID.mom"
        }
    }

Note how the "moments" entry is properly aligned as a table.

This is what I have for trying to get this information into a Label:

root = tk.Tk()
root.title("PyMolDat")
num = 0

json = {
        "H3": {
            "atom": "H3",
            "scheme": "NH3_ISA-GRID",
            "type": "HN",
            "rank": "4",
            "moments": [
                "          Q0        Q1        Q2        Q3        Q4",
                "0   0.353619 -0.000000  0.022593 -0.000000  0.016054",
                "1s       NaN -0.020984 -0.000000 -0.010761 -0.000000",
                "1c       NaN -0.009221 -0.000000  0.007970 -0.000000",
                "2s       NaN       NaN -0.016711 -0.000000  0.015248",
                "2c       NaN       NaN  0.016692 -0.000000 -0.009410",
                "3s       NaN       NaN       NaN  0.003688 -0.000001",
                "3c       NaN       NaN       NaN  0.025270 -0.000001",
                "4s       NaN       NaN       NaN       NaN  0.005240",
                "4c       NaN       NaN       NaN       NaN  0.010030"
            ],
            "file": "/Users/gianluca/Desktop/project/example_molecules/ISA/OUT/NH3_ISA-GRID.mom"
        }
    }

for k, v in json.items():
    for i, j in v.items():

        tk.Label(root, text=i, width=10, anchor="w", font="Arial 10 bold").grid(row=num,
                                                                                column=0, padx=10, sticky="ne")

        tk.Label(root, text=j if i != "moments" else "\n".join(j), width=65, anchor="w", justify='left').grid(
            row=num, column=1, padx=5)

        num += 1

root.mainloop()

and the resulting table instead loses the appropriate spacing, see image 1:

image_1

Any ideas on how to format the "moments" text block? Many thanks for your time and effort. I've just noticed the information in json isn't the same as image 1 but of course everything is the same, my bad, shouldn't change anything however.

Use monospaced font , eg font=("Lucida Console", 10) :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM