| Tk-Sugar documentation | Contained in the Tk-Sugar distribution. |
( -fill => 'x' )( -fill => 'y' )( -fill => 'both' )fillx with ( -expand => 1 )fillyfill2( -expand => 1 ) if you don't like
the xfill* notation
( -padx => 1, -pady => 1 )( -ipadx => 1, -ipady => 1 )Tk::Sugar - Sugar syntax for Tk
version 1.093190
use Tk::Sugar qw{ :pack :state };
$widget->pack( top, xfill2, pad10 );
# equivalent to those pack options:
# -side => 'top'
# -expand => 1
# -fill => 'both'
# -padx => 10
# -pady => 10
$widget->configure( enabled );
# equivalent to: -state => 'enabled'
Tk is a great graphical toolkit to write desktop applications. However, one can get bothered with the constant typing of quotes and options. Tk::Sugar provides handy subs for common options used when programming Tk.
Benefits are obvious:
The constant need to type => and '' is fine for one-off cases,
but the instant you start using Tk it starts to get annoying.
Reduces much of the redundant typing in most cases, which makes your life easier, and makes it take up less visual space, which makes it faster to read.
Strings are often problematic, since they aren't checked at compile- time. Sometimes it makes spotting an error a difficult task. Using this alleviates that worry.
This module is using Sub::Exporter underneath, so you can use all its shenanigans to change the export names.
Look below for the list of available subs.
Traditional packer sides (available as :side export group):
( -side => 'top' )bottomleftrightPacker expand and filling (available as :fill export group):
( -fill => 'x' )( -fill => 'y' )( -fill => 'both' )fillx with ( -expand => 1 )fillyfill2( -expand => 1 ) if you don't like
the xfill* notationPacker padding (available as :pad export group):
( -padx => 1, -pady => 1 )Packer padding (available as :ipad export group):
( -ipadx => 1, -ipady => 1 )Widget state (available as :state export group):
( -state => 'normal' )disabledWidget anchor (available as :anchor export group). Note that those
subs are upper case, otherwise the sub s would clash with the regex
substitution:
( -anchor => 'n' )sewcenternenwseswWidget orientation (available as :orient export group).:
( -orient => 'horizontal' )verticalBeside the individual groups outlined above, the following export groups exist for your convenience:
This exports all existing subs.
This exports subs related to Tk::pack options. Same as :side,
:fill, :pad and :ipad.
This exports subs related to widget configure options. Same as
:state, :anchor and :orient.
You can look for information on this module at:
Jerome Quelin
This software is copyright (c) 2009 by Jerome Quelin.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
| Tk-Sugar documentation | Contained in the Tk-Sugar distribution. |
# # This file is part of Tk-Sugar # # This software is copyright (c) 2009 by Jerome Quelin. # # This is free software; you can redistribute it and/or modify it under # the same terms as the Perl 5 programming language system itself. # use 5.008; use strict; use warnings; package Tk::Sugar; our $VERSION = '1.093190'; # ABSTRACT: Sugar syntax for Tk use Sub::Exporter -setup => { exports => [ qw{ top bottom left right fillx filly fill2 xfillx xfilly xfill2 expand pad pad1 pad2 pad5 pad10 pad20 padx pady ipad ipad1 ipad2 ipad5 ipad10 ipad20 ipadx ipady enabled disabled N S E W C NE NW SE SW horizontal vertical } ], groups => { fill => [ qw{ fillx filly fill2 xfillx xfilly xfill2 expand } ], side => [ qw{ top bottom left right } ], pad => [ qw{ pad pad1 pad2 pad5 pad10 pad20 padx pady } ], ipad => [ qw{ ipad ipad1 ipad2 ipad5 ipad10 ipad20 ipadx ipady } ], pack => [ qw{ -fill -side -pad -ipad } ], state => [ qw{ enabled disabled } ], anchor => [ qw{ N S E W C NE NW SE SW } ], orient => [ qw{ horizontal vertical } ], options => [ qw{ -state -anchor -orient } ], default => [ qw{ -pack -options } ], } }; ## no critic (ProhibitSubroutinePrototypes) # -- pack options # cf perldoc Tk::pack for more information # pack sides sub top () { return ( -side => 'top' ); } sub bottom () { return ( -side => 'bottom' ); } sub left () { return ( -side => 'left' ); } sub right () { return ( -side => 'right' ); } # pack fill / expand sub fillx () { return ( -fill => 'x' ); } sub filly () { return ( -fill => 'y' ); } sub fill2 () { return ( -fill => 'both' ); } sub xfillx () { return ( -expand => 1, -fill => 'x' ); } sub xfilly () { return ( -expand => 1, -fill => 'y' ); } sub xfill2 () { return ( -expand => 1, -fill => 'both' ); } sub expand () { return ( -expand => 1 ); } # padding sub pad1 () { return ( -padx => 1, -pady => 1 ); } sub pad2 () { return ( -padx => 2, -pady => 2 ); } sub pad5 () { return ( -padx => 5, -pady => 5 ); } sub pad10 () { return ( -padx => 10, -pady => 10 ); } sub pad20 () { return ( -padx => 20, -pady => 20 ); } sub pad { my $n=shift; return ( -padx => $n, -pady => $n ); } sub padx { my $n=shift; return ( -padx => $n ); } sub pady { my $n=shift; return ( -pady => $n ); } # internal padding sub ipad1 () { return ( -ipadx => 1, -ipady => 1 ); } sub ipad2 () { return ( -ipadx => 2, -ipady => 2 ); } sub ipad5 () { return ( -ipadx => 5, -ipady => 5 ); } sub ipad10 () { return ( -ipadx => 10, -ipady => 10 ); } sub ipad20 () { return ( -ipadx => 20, -ipady => 20 ); } sub ipad { my $n=shift; return ( -ipadx => $n, -ipady => $n ); } sub ipadx { my $n=shift; return ( -ipadx => $n ); } sub ipady { my $n=shift; return ( -ipady => $n ); } # -- common widget options # cf perldoc Tk::options for more information # widget state sub enabled () { return ( -state => 'normal' ); } sub disabled () { return ( -state => 'disabled' ); } # anchor sub N () { return ( -anchor => 'n' ); } sub S () { return ( -anchor => 's' ); } sub E () { return ( -anchor => 'e' ); } sub W () { return ( -anchor => 'w' ); } sub C () { return ( -anchor => 'center' ); } sub NE () { return ( -anchor => 'ne' ); } sub NW () { return ( -anchor => 'nw' ); } sub SE () { return ( -anchor => 'se' ); } sub SW () { return ( -anchor => 'sw' ); } # orientation sub horizontal () { return ( -orient => 'horizontal' ); } sub vertical () { return ( -orient => 'vertical' ); } 1;
__END__