Perl/Tkx:Tk风格 VS Perl OO 风格

抱雪 posted @ 2012年10月17日 21:30 in Perl , 5839 阅读
  •  

原文地址:http://www.tkdocs.com/tutorial/concepts.html

What's somewhat scary about the Tkx module is that it implements all of this on a purely syntactic level. That is, it has no clue about buttons and entries and grid. It just knows that if it sees a method "new_something" it's creating a widget using the Tcl command "something", or if you call a method "g_otherthing", that it's going to invoke a Tcl command "otherthing", and pass the widget pathname associated with the object as a first parameter (followed by any other parameters passed to the method).

Tcl 命令 "somwthing" 写成 $父元件->new_something,也可以用 g_something(pathname)

It's this pure syntactic mapping which gives Tkx its power and amazing brevity (check out the code in Tkx.pm!). It also explains why it can automatically track any changes in Tk. Any new commands and options don't need to be explicitly implemented in Tkx code; they are automatically available just by using the right syntax.

Now let's talk about those underscores in the Tkx commands and method names. If you've glanced at the Tcl version of the code in this tutorial, you know that they can take several forms, i.e.

  • a single toplevel command, like "grid"
  • a multi-word command (called an ensemble in Tcl), e.g. "wm title"
  • a command in a namespace, e.g. "ttk::button"
  • a single toplevel command with underscores, e.g. "tk_messageBox"

No translations are needed for simple commands like "grid", but the others need to be tweaked. To translate these into Perl, Tkx uses underscores in the following way:

  • a single underscore is replaced with a space, e.g. "wm_title" in Perl becomes "wm title" in Tcl
  • two underscores is replaced with the namespace qualifier "::", e.g. "ttk__button" becomes"ttk::button"
  • three underscores is replaced with a single underscore, e.g. "tk___messageBox" becomes"tk_messageBox"

转换规则如下:

  • 单个单词命令的不用转换,如 grid
  • 多个单词的命令用下划线连接,如 wm title 写成 wm_title
  • 名称空间的 :: 写成两个下划线,如 ttk::button 写成 ttk__button
  • 一个下划线写成3个下划线,如 tk_messageBox 写成 tk___messageBox

程序对比:

use strict;
use warnings;
use Tkx;
use utf8;
my ($feet,$meters);
Tkx::wm_title('.','Feet to Metes');
Tkx::ttk__frame('.c', -padding => '3 3 12 12');
Tkx::grid('.c', -column => 0, -row => 0, -sticky => 'news');
Tkx::grid_columnconfigure('.', 0, -weight => 1);
Tkx::grid_rowconfigure('.', 0, -weight => 1);

Tkx::ttk_entry('.c.feet', -width => 7, -textvariable => \$feet);
Tkx::grid('.c.feet', -column => 2, -row => 1, -sticky => 'we');
Tkx::ttk__label('.c.meters', -textvariable => \$meters);
Tkx::grid('.c.meters', -column => 2, -row => 2, -sticky => 'we');
Tkx::button('.c.calc', -text => 'Calculate', -command => sub{&calculate});
Tkx::grid('.c.calc', -column => 3, -row => 3, -sticky => 'w');

Tkx::grid(Tkx::ttk__label('.c.flbl', -text => 'feet'), -column => 3, -row =>1, -sticky => 'w');
Tkx::grid(Tkx::ttk__label('.c.islbl', -text => 'is equivalent to'), -column => 1, -row =>2, -sticky => 'e');
Tkx::grid(Tkx::ttk__label('.c.mlbl', -text => 'meters'), -column => 3, -row =>2, -sticky => 'w');

for (Tkx::SplitList(Tkx::winfo_children('.c'))) {
    Tkx::grid_configure($_, -padx => 5);
}

Tkx::focus('.c.feet');
Tkx::bind('.', '<Return>', \&calculate);

sub calculate {
    $meters = int(0.3048*$feet*10000.0+.5)/10000.0 || '';
}
Tkx::MainLoop;

 

use Tkx;

my $mw = Tkx::widget->new(".");
$mw->g_wm_title("Feet to Meters");
my $frm = $mw->new_ttk__frame(-padding => "3 3 12 12");
$frm->g_grid(-column => 0, -row => 0, -sticky => "nwes");
$mw->g_grid_columnconfigure(0, -weight => 1);
$mw->g_grid_rowconfigure(0, -weight => 1);

my $ef = $frm->new_ttk__entry(-width => 7, -textvariable => \$feet);
$ef->g_grid(-column => 2, -row => 1, -sticky => "we");
my $em = $frm->new_ttk__label(-textvariable => \$meters);
$em->g_grid(-column => 2, -row => 2, -sticky => "we");
my $cb = $frm->new_ttk__button(-text => "Calculate", -command => sub {calculate();});
$cb->g_grid(-column => 3, -row => 3, -sticky => "w");

$frm->new_ttk__label(-text => "feet")->g_grid(-column => 3, -row => 1, -sticky => "w");
$frm->new_ttk__label(-text => "is equivalent to")->g_grid(-column => 1, -row => 2, -sticky => "e");
$frm->new_ttk__label(-text => "meters")->g_grid(-column => 3, -row => 2, -sticky => "w");

foreach (Tkx::SplitList($frm->g_winfo_children)) {
    Tkx::grid_configure($_, -padx => 5, -pady => 5);
}
$ef->g_focus;
$mw->g_bind("<Return>", sub {calculate();});

sub calculate {
   $meters = int(0.3048*$feet*10000.0+.5)/10000.0 || '';
}

Tkx::MainLoop();

 

Avatar_small
Abigail Connor 说:
2018年11月29日 10:57

All the posts of the hugs bury have been identified for the candidates. The transfer of the motives and superior papers review is vital for the formation for the parts. The improvement is done for the humans for the use of the fabricated items and all ideal paths for the humans in life.

Avatar_small
seo service UK 说:
2024年1月16日 16:37

Amazing experience after visiting this site which is really full of information and someone have information for


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter