[Tkinter-discuss] Right justify Label against Entry using pack?

python at bdurham.com python at bdurham.com
Thu Dec 2 02:17:20 CET 2010


I'm experimenting with geometry layouts and would like to know if
its possible to line up a Label and an Entry widget (in the same
horizontal line) and have the Label widget right align to the
Entry widget. I know I can do this via grid, but I'm wondering if
this is possible via pack.

Here's the 1 line layout I'm trying to achieve:

     [Label] [Entry        ]
^^^
Where the ^^^ indicates leading space due to right alignment of
the label against its Entry.

Listed below is my test code. I've tried using Tkinter's Label
and ttk's Label. I've tried using anchor, justify, and fill in
various combinations without success. One interesting
observation: the ttk Label widget always seems to align left in
the scenario below while I can get the Tkinter Label widget to
center or left align, but never right align.

Perhaps I'm confused about when to use justification vs.
alignment to achieve my goal?

Your thoughts appreciated!

import Tkinter as tk
import ttk
root = tk.Tk()
frame = tk.Frame( root )

parent = frame
label = tk.Label( parent )

# note that I'm explictly setting width to create an environment
for alignment or justification
label.config( text='Password', width=20, justify='right' )
label.pack( side='left', anchor='e', padx=4, pady=4 )
entry = ttk.Entry( parent )
entry.pack( side='left', anchor='w', fill='x', expand=True,
padx=4, pady=4 )
frame.pack( side='top', anchor='n', fill='x', expand=True,
padx=4, pady=4 )

root.mainloop()

Malcolm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20101201/ab4b1053/attachment-0001.html>


More information about the Tkinter-discuss mailing list