如何在自定义 Tkinter 中对齐这些数字

问题描述 投票:0回答:1

每当我运行我的代码时,一切都排列整齐 输出(在 vscode / 终端中):

Preview 1 2 3 4 5 6 7 8 9  R   H   E  
Astros  0                  0   0   0  
Cubs    0                  0   0   0  

但是当我在 CTk 中运行代码时,运行数、点击数和错误数不再排列:

Preview 1 2 3 4 5 6 7 8 9  R   H   E  
Astros  0           0   0   0  
Cubs    0            0   0   0  

(娱乐,当我从文本框中复制并粘贴时,它正常排列)

vvv CTk 代码 vvv

import customtkinter
import statsapi
import datetime
import time
import os
    
date = datetime.datetime.now().strftime("%m/%d/%Y")
astros_schedule = statsapi.schedule(team=117,start_date=date,end_date=date)
gamepk = astros_schedule[0]['game_id']
astros_linescore = statsapi.linescore(gamePk=gamepk)

customtkinter.set_appearance_mode("Dark")
customtkinter.set_default_color_theme("dark-blue")


app = customtkinter.CTk()
app.title("Astros Score Manual")
app.grid_columnconfigure(0, weight=1)
app.geometry("650,325")
switch_var = customtkinter.StringVar(value="off")
def switch_event():
    onoff = switch_var.get()
    if onoff == "off":
        Textbox.delete("0.0","end")
    elif onoff == "on":
        Textbox.configure(state="normal")
        Textbox.insert("0.0", astros_linescore)
        Textbox.configure(state="disabled")






Switch = customtkinter.CTkSwitch(master=app, text="CTkSwitch", command=switch_event,
                                   variable=switch_var, onvalue="on", offvalue="off")
Switch.pack(padx=20, pady=10)

Textbox = customtkinter.CTkTextbox(master=app,width=675,height=325, corner_radius=20, border_width=5, activate_scrollbars = False, state="disabled",font=("sans-serif",14), wrap="none")
Textbox.pack(padx=20, pady=10)





app.mainloop()

抱歉,如果这是冗长或令人困惑的,这是我第一次问问题。

我尝试摆弄字体设置,但没有任何改变

python python-3.x textbox alignment customtkinter
1个回答
0
投票

你必须使用一些

monospace
字体,它的所有字符具有相同的宽度。

维基百科:等宽字体列表 - 维基百科


带有一些

monospace
字体的最小工作代码。

import customtkinter

app = customtkinter.CTk()
app.geometry("650x325")

for font in ('monospace', 'Courier', 'Ubuntu Mono'):

    textbox = customtkinter.CTkTextbox(app, font=(font, 14), height=100)
    textbox.pack(fill="both")

    textbox.insert("end", f"""FONT: {font}
    Preview 1 2 3 4 5 6 7 8 9  R   H   E  
    Astros  0                  0   0   0  
    Cubs    0                  0   0   0""")

app.mainloop()

© www.soinside.com 2019 - 2024. All rights reserved.