Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text in tkinter refuses to left justify

I have some code that outputs some mass spec data statistics, e.g.

    r43_header_label = tk.Label(stats_frame, text='4/3 Ratio')
    r43_header_label.config(font=('System', 20, 'bold'))
    r43_header_label.pack()
    if analysis.ratio43_t0int > 0.01:
        r43_t0int_label  = tk.Label(stats_frame, text=f'  t₀: {analysis.ratio43_t0int:.3f} ± {analysis.ratio43_uncert:.3e}', anchor='w', justify='left')
    else:
        r43_t0int_label  = tk.Label(stats_frame, text=f'  t₀: {analysis.ratio43_t0int:.3e} ± {analysis.ratio43_uncert:.3e}', anchor='w', justify='left')
    r43_t0int_label.pack()
    r43_t0err_label  = tk.Label(stats_frame, text=f'  error: {analysis.ratio43_puncert:.3f}%', anchor='w', justify='left')
    r43_t0err_label.pack()

I thought surely between anchoring the text to 'w'est and using justify='left' would convince tkinter that I want the text to be left justified, however, here is an example of the results, which are clearly centered.

Besides the fact that the +/- error is vastly lower than the % error, (I’m working on that too), what am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Even better, how could I have the text be left justified and the numbers all start in a neat row, e.g.

t0:       0.75
error:    2.356%
mean:     1.5

>Solution :

Appropriately setting the fill option for each label’s pack call should cause them to align properly

r43_header_label.pack(fill='x')
...
r43_t0int_label.pack(fill='x')
...
r43_t0err_label.pack(fill='x')

If you want two columns for your data wherein each row has a name label and an associated value, an easy if naïve approach is to encapsulate two labels in a Frame and pack them accordingly

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading