Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GUI problem: "-justify" doesn't work in the LABEL widget? 1

Status
Not open for further replies.

sloppyhack

Technical User
Apr 17, 2001
111
US
#!/usr/bin/perl -w
use strict;

use Tk;
my $mw = MainWindow->new();
$mw->Label(-text => "TESTING", -background => 'white', -width => 50, -justify => 'left')->pack();

MainLoop;

This code should create a label with the text on the left side. Doesn't work. Puts it in the center. This must be a bug. Anyone know anything about it?



Cheers,

Sloppyhack
 
Calling pack() by itself centers all widgets. The text is properly left-aligned within the widget itself, but the widget itself is centered so you can't see it.

When you pack it you should anchor it. The default anchor seems to be "n" (or, the north side of the widget, which refers to the center point along the top). If you pack it with an anchor of "w", it sticks it into the left side of its parent widget.

Code:
use Tk;

my $mw = MainWindow->new;

foreach (qw(something to write here real quickly)) {
	$mw->Label (
		-text => $_,
		-justify => 'left',
	)->pack (-anchor => 'w');
}

MainLoop;
 
Thanks for the suggestion but I beleive that is just putting the widget (and its text) on the left side. It is not changing the placement of the text within the widget. Changing the code you gave me to "-justify => 'right'" produces the same result.

I really think this is just a bug. In the original code I provided, I set the frame background to white so you can see the entire label to get a perspective of what it's doing with the text. You can change the "justify" to "left" or "right" and it always centers within the frame.

Cheers,

Sloppyhack
 
Here's a better demonstration:

Code:
$mw->Label (
   -text => "xxxxxxxxxxxxxxx\n"
      . "xxx\n"
      . "xxxxxxxx\n"
      . "xx",
   -justify => 'left',
)->pack;

The widgets tend to use the minimum width and height possible, so justification shows up the best when it has multiple lines of varying lengths.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top