0

Whenever I run my code everything is lined up Output (in vscode / terminal):

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

But when ever I run my code in CTk the Runs, Hits and, Error numbers don't line up anymore:

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

(recreation as when I copy and paste from the textbox it is lined up normally)

vvv CTk Code 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()

Sorry if this is long winded or confusing this is my first time asking a question.

Ive tried fiddling with the font settings and nothing change

2
  • you may have to use some monospace font Commented Apr 23, 2024 at 22:48
  • @furas thank you this worked changing it to Courier although it is quite bad looking in the text box, if you want can you post your comment as an answer so i can mark it Commented Apr 23, 2024 at 23:13

1 Answer 1

0

You have to use some monospace font which has all chars with the same width.

Wikipedia: List of monospaced typefaces - Wikipedia


Minimal working code with some monospace fonts.

import customtkinter

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

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

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

    textbox.insert("end", f"""FONT: {font_name}
    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()

enter image description here


EDIT:

Using tkinter.Text you can use tags to format text in one widgets

import tkinter as tk

app = tk.Tk()
app.geometry("650x325")

textbox = tk.Text(app, height=300)
textbox.pack(fill="both")

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

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

    textbox.tag_config(font_name, font=(font_name, 14))

app.mainloop()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.