如何更改Tkinter标签中文本的文本间距?

时间:2019-02-22 12:49:30

标签: python json text tkinter

我有一个标签,需要显示以下信息:

    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"
        }
    }

请注意“矩”条目如何正确对齐为表格。

这就是我试图将这些信息添加到标签中的目的:

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()

,结果表失去适当的间距,请参见图1:

image_1

关于如何格式化“时刻”文本块的任何想法?非常感谢您的时间和精力。我刚刚注意到json中的信息与图片1不同,但是当然所有内容都是一样的,但是我不应该更改任何内容。

1 个答案:

答案 0 :(得分:2)

使用monospaced font,例如font=("Lucida Console", 10)

enter image description here