Python Forum
Column allignment off so slightly using ljustify()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Column allignment off so slightly using ljustify()
#1
issue: columns become slightly miss-aligned- not a whole character(1/4-1/2 maybe) but enough to tell and look bad.
situation: taking returned data from a sort and using below to take each row and format it and add to a string to add to a text input box.
I am weak on string formatting but when i step through record by record and field by field and record the math between each column spacing, it equates correctly- taking into account the text length and spacing.
Question: Could a empty field somehow impart a few pixels off? I wanted to attach a screen shot of the misalignment but could not figure out how to do that-sorry.

A couple of rows of returned data (animals)look similar to below with some fields typicially having no data:
-('4','Yellow','','Bull Lot',11,'','')
-('23','Yellow','2018','Hermas',20,'','')

def build_cow_sc(self,animals):
        self.cow_abbr_txt.text=''
        for cow in animals:
            t=''
            item=0
            while item < len(cow):
                t=t+str(str(cow[item]).ljust(40-len(str(cow[item]))))#+"     "
                item+=1
            self.cow_abbr_txt.text=self.cow_abbr_txt.text +'\n'+ t   
Reply
#2
Upon closer examination, the character count is correct and the same between columns, however; the width of the characters are what shifts the text row over row. So the width for a character "Y" is not the same for "i".

How would one compensate for this if you were spacing out a string?
Reply
#3
Use a monospace font like Courier.

Better yet, don't use a text box to show tabular data. Use something that has columns. In tkinter you could make a grid of text boxes that are arranged in columns. Most other GUI toolkits have a table view widget.
Reply
#4
There are a lot option to format stuff in Python,here some ways using your data.
# pip install tabulate
# pip install pandas
from tabulate import tabulate
import pandas as pd

lst = [
    ('4','Yellow','','Bull Lot', '11'),
    ('23','Yellow','2018','Hermas', '20')
]

print(tabulate(lst))
print(tabulate(lst, tablefmt="fancy_grid"))

# f-string
for inner in lst:
    print(' | '.join((f"{word:^8}" for word in inner)))

print('-' * 45)
df = pd.DataFrame(lst)
print(df)
Output:
-- ------ ---- -------- -- 4 Yellow Bull Lot 11 23 Yellow 2018 Hermas 20 -- ------ ---- -------- -- ╒════╤════════╤══════╤══════════╤════╕ │ 4 │ Yellow │ │ Bull Lot │ 11 │ ├────┼────────┼──────┼──────────┼────┤ │ 23 │ Yellow │ 2018 │ Hermas │ 20 │ ╘════╧════════╧══════╧══════════╧════╛ 4 | Yellow | | Bull Lot | 11 23 | Yellow | 2018 | Hermas | 20 --------------------------------------------- 0 1 2 3 4 0 4 Yellow Bull Lot 11 1 23 Yellow 2018 Hermas 20

Rich 🌞✨
from rich.console import Console
from rich.table import Table

lst = [
    ('4','Yellow','','Bull Lot', '11'),
    ('23','Yellow','2018','Hermas', '20')
]

table = Table(show_header=True, header_style='blue bold')
table.add_column("Id", justify="right", style="yellow", no_wrap=True)
table.add_column("Color", style="magenta")
table.add_column("Year", justify="right", style="magenta")
table.add_column("Name", style="magenta")
table.add_column("Numb", justify="right", style="yellow")
for row in lst:
    table.add_row(*row)

console = Console()
console.print(table, style="green")
[Image: 3PhQpD.png]
Reply
#5
I could be wrong, but I think the OP is creating strings to put in a tkinter text box. Can you use tabulate to create a str that is compatible with a text box?
Reply
#6
(Apr-21-2022, 04:28 PM)deanhystad Wrote: I could be wrong, but I think the OP is creating strings to put in a tkinter text box. Can you use tabulate to create a str that is compatible with a text box?
Yes,you may be right i did read it a little fast 🚀
Reply
#7
Thanks all.
The mono text resolved the issue but good to know there are a lot of ways to handle this.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020