簡體   English   中英

如何更改Tkinter標簽中文本的文本間距?

[英]How to change text spacing for text in a Tkinter Label?

我有一個標簽,需要顯示以下信息:

    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不同,但是當然所有內容都是一樣的,我的意思是,但是不要更改任何內容。

使用等寬字體 ,例如font=("Lucida Console", 10)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM