[Python-ideas] Format character to center strings
David Blaschke
dwblas at gmail.com
Sat Mar 28 18:11:49 CET 2015
Centered=same amount of white space on the left and to the right of
the text. This depends on the font being used, especially whether it
is a fixed or proportional font. So you would possibly have to also
specify the font face and font size being used. Programming is not
like a word processor where the font is specified within each
document. Run this program to see the display difference between two
"sentences" that are the same length, and between proportional and
fixed fonts.
#! /usr/bin/python
try:
import Tkinter as tk ## Python 2.x
except ImportError:
import tkinter as tk ## Python 3.x
class DifferentFonts():
def __init__(self):
self.top=tk.Tk()
lit_1="Display lots of i's and l's-iiilll"
lit_2="This is the same length as 1-wwwqq"
ctr = 0
for each_font in (('Verdana', 12),
('Fixed', 12)):
tk.Label(self.top, text=each_font[0]+"-"*30,
font=each_font).grid(row=ctr)
ctr += 1
tk.Label(self.top, text=lit_1,
font=each_font).grid(row=ctr, sticky="w")
ctr += 1
tk.Label(self.top, text=lit_2,
font=each_font).grid(row=ctr, sticky="w")
ctr += 1
tk.Button(self.top, text="Quit", bg="orange",
command=self.top.quit).grid(row=20)
self.top.mainloop()
DifferentFonts()
On 3/28/15, Rob Cliffe <rob.cliffe at btinternet.com> wrote:
> As a string can be left-justified (within a given width) using e.g.
> '%-20s' % s
> and right-justified using
> '%+20s' % s # or '%20s' % s
> why not allow a string to be centered using
> '%=20s' %s
>
> Rob Cliffe
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--
Only when you actually get to the state where there is neither
delusion nor enlightenment are you finally comfortable...Foyan
More information about the Python-ideas
mailing list