简体   繁体   English

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

[英]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: 而结果表失去适当的间距,请参见图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. 我刚刚注意到json中的信息与图片1不同,但是当然所有内容都是一样的,我的意思是,但是不要更改任何内容。

Use monospaced font , eg font=("Lucida Console", 10) : 使用等宽字体 ,例如font=("Lucida Console", 10)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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