[Tutor] Config options on Tkinter Label Widget

Rick Pasotto rick@niof.net
Wed, 4 Jul 2001 21:57:19 -0400


On Wed, Jul 04, 2001 at 05:55:25PM -0700, Sheila King wrote:
> I'm trying my hand at a bit of Tkinter programming. I must say, the
> discussion in Mark Lutz' Programming Python, 2nd ed, is really
> excellent.
> 
> Anyhow, I liked the suggestion in Chun's Core Python Programming for one
> of the exercises: "Label and Entry Widgets and Python I/O: Create a GUI
> application that provides and Entry field where the suer can provide the
> name of a text file. Open the file and read it, displaying its contents
> in a Label."
> 
> So, I'm just starting out on the exercise, and I decided to mess around
> a bit with the look and feel of the application. (I've also snagged a
> local copy of Lundh's Tkinter reference and the Tkinter Life
> Preserver...so I think I have ample reference material.)
> 
> So, here is my code so far:
> --------------------------------------------------------------
> 
> from Tkinter import *
> 
> root = Tk()
> Directions = Label(root, text ='Enter name of File to Open:')
> Directions.config(anchor=W)
> inputBox = Entry(root, width=80)
> 
> Directions.pack()
> inputBox.pack()
> mainloop()
> --------------------------------------------------------------
> 
> Now, notice the line where I am trying to configure on Directions the
> positioning of the text in the label:
> Directions.config(anchor=W)
> 
> >From reading Lundh's reference, I thought that this would position the
> text all the way to the left of the label.
> 
> However, it remains centered.
> 
> Why isn't it moving to the left? What do I need to do to get that
> effect?

Actually it *is* positioned to the left of the label. The problem is
that the label is only as wide as the text. You need to add two things
to the pack command: 'expand=1' to say you want the label to fill the
available space, and 'fill=X' to say you want the label to expand
horizontally.

Directions.pack(expand=1,fill=X)

To get a better idea of what is happening first color the label:

Directions.config(anchor=W,bg='yellow')

and then try adding a width:

Directions.config(anchor=W,bg='yellow',width=70)

(Be sure you do the above with the original Directions.pack().)

-- 
The ideological war now being waged against property is neither
the most bitter nor the most dangerous that it has had to contend
with. Since the beginning of the world there has also been a real
war of violence and conspiracy waged against it that gives no sign
of abating. War, slavery, imposture, inequitable taxation,
monopoly, privilege, unethical practice, colonialism, the right to
employment, the right to credit, the right to education, the right
too public aid, progressive taxation in direct or inverse ratio to
the ability to pay -- all are so many battering rams pounding
against the tottering column. Could anyone assure me whether there
are many men in France, even among those who consider themselves
conservatives, who do not, in one form or another, lend a hand to
this work of destruction.
	-- Frédéric Bastiat (1801-1850)
    Rick Pasotto    rickp@telocity.com    http://www.niof.net