5

I have been working on a GUI using Perl Tkx, and discovered that there are two separate functions you can use to create buttons. (button and ttk__button).

So far the only difference I found is that the button function appears to center justify the text while the ttk__button function appears to left justify the text.


Example using button:

#!/usr/bin/perl

use strict;
use warnings;

use Tkx;

my $text = "Hello\nworld!\n\nThis is some text...";
my $mw = Tkx::widget->new(".");

my $b = $mw->new_button(
    -text => $text,
    -command => sub { exit; },
);
$b->g_grid;

Tkx::MainLoop()

This script will center the text:

Centered text


Example using ttk__button:

#!/usr/bin/perl

use strict;
use warnings;

use Tkx;

my $text = "Hello\nworld!\n\nThis is some text...";
my $mw = Tkx::widget->new(".");

my $b = $mw->new_ttk__button(
    -text => $text,
    -command => sub { exit; },
);
$b->g_grid;

Tkx::MainLoop()

This script will left justify the text:

Left justified text


I was wondering what other differences there are between these two functions and whether there is any particular reason there are two separate functions that create buttons. Is one of these typically better to use than the other?

Also is there any way to center justify the text using the ttk__button function?

EDIT:

I added the "Tcl" tag to this since Perl Tkx is essentially built off of Tcl. I'm pretty good with Perl but I am somewhat of a novice when it comes to Tcl.

Although it may not seem like it at first glance, this is actually more of a Tcl question than a Perl question so I would love to hear an answer from someone who is experienced with Tcl.


NOTE:

To center text in a ttk__button add the following line to your code:

Tkx::ttk__style_configure('TButton', -justify => 'center');

tjwrona1992
  • 8,614
  • 8
  • 35
  • 98

2 Answers2

5

Button is one of the, let's say, 'traditional' widgets. The ttk family of widgets are more modern and are designed to pay attention to the display configuration of the Host OS. If you want your app to blend in with your user's chosen 'theme' then use the ttk widgets. On the other hand, if you like to do lots of work to fit in, or would like your application to stick out like a sore thumb, then by all means use the 'traditional' widgets.

Finer control of ttk widgets can be exerted via the -style option. You may want to look at the tutorial page @ http://www.tkdocs.com/tutorial/styles.html .

For what it's worth, buttons do not frequently contain multi-line text. There are other widgets that are designed with text layout in mind.

EDIT:

From tjwrona1992's comment: Tkx::ttk__style_configure('TButton', -justify => 'center');

tjd
  • 4,064
  • 1
  • 24
  • 34
  • Okay that's exactly what I was wondering, but is there a way to center the text on a `ttk button`? When I use multiple lines of text and it aligns it all left it looks ugly. – tjwrona1992 Jan 12 '15 at 20:22
  • Personally, I'd reevaluate the design that places multi-line text in a button. Finer control of `ttk` widgets can be exerted via the `-style` attribute. – tjd Jan 12 '15 at 20:28
  • Well I have a button that needs to fit in a relatively small space. Even if it had its own way to wordwrap the text that would be useful. Right now if I don't use "\n" to make it cross multiple lines it will put it all in one line and stretch the display to fit it which does not look very good either. – tjwrona1992 Jan 12 '15 at 20:33
  • You're sure you don't have room for a frame containing a button with minimal text and an accompanying label? – tjd Jan 12 '15 at 20:37
  • The button has a few words on it that when aligned with everything else look better if they go across 2 lines. Ultimately I would like the `ttk__button` function to center the text exactly like the `button` function does. I'm sure there is some other way I could make it look nice without doing this, but at this point i'm just curious whether it can be done. – tjwrona1992 Jan 12 '15 at 20:44
  • 3
    Thanks for the tip on using `style`! after playing around with it a bit I got it to work. `Tkx::ttk__style_configure('TButton', -justify => 'center');` – tjwrona1992 Jan 12 '15 at 21:05
2

Tk has two classes of widgets: traditional and tiled. Traditional widgets are those that have been around since Tk's inception. Tiled widgets are relatively new. They emulate native widgets via support for themes. Many (but not all) widgets are available in both forms. Tile started as a separate library before being integrated into Tk 8.5 as Ttk.

Unfortunately, switching from traditional to tiled Tk widgets isn't as simple as adding a "use ttk" statement because traditional and tiled widgets don't necessarily have the same set of configuration options.

In Tkx, The ttk prefix is used to specify the tiled version of a widget. The Tcl Tk wiki has a comparison of ttk widgets using various themes. (The "classic" theme matches the look & feel of traditional Tk.) Here's how button and ttk__button render on my system (Win7):

button vs ttk button

Traditional widgets are more flexible. You can configure more attributes and do so on a per-widget basis. Tiled widgets look better (i.e. native) and make it easier to change the overall look & feel of your application.

Personally, I always use the ttk widgets when available.

Michael Carman
  • 30,628
  • 10
  • 74
  • 122