Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Tkx, What is the Difference Between "button" and "ttk__button"?

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');

like image 387
tjwrona1992 Avatar asked Jan 12 '15 17:01

tjwrona1992


2 Answers

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');

like image 174
tjd Avatar answered Nov 15 '22 07:11

tjd


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.

like image 39
Michael Carman Avatar answered Nov 15 '22 09:11

Michael Carman