tutorial.html
来自「perl教程」· HTML 代码 · 共 566 行 · 第 1/4 页
HTML
566 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Tkx::Tutorial - How to use Tkx</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>Tkx::Tutorial - How to use Tkx</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#hello_world">Hello World</a></li>
<li><a href="#hello_world_with_objects">Hello World with objects</a></li>
<li><a href="#hello_world_expanded">Hello World expanded</a></li>
<li><a href="#setting_up_a_menu_line">Setting up a menu line</a></li>
<li><a href="#using_tcl_packages">Using Tcl packages</a></li>
<li><a href="#subclassing_tkx__widget">Subclassing Tkx::widget</a></li>
</ul>
<li><a href="#license">LICENSE</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Tkx::Tutorial - How to use Tkx</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><em>Tk</em> is a toolkit that allows you to create applications with
graphical interfaces for Windows, Mac OS X and X11. The Tk toolkit
is native to the <em>Tcl</em> programming language, but its ease of use and
cross-platform availability has made it the GUI toolkit of choice for
many other dynamic languages as well.</p>
<p><em>Tkx</em> is a Perl module that makes the Tk toolkit available to Perl
programs. By loading the Tkx module Perl programs can create
windows and fill them with text, images, buttons and other controls
that make up the user interface of the application.</p>
<p>
</p>
<h2><a name="hello_world">Hello World</a></h2>
<p>Let's start with the mandatory exercise of creating an application
that greats the world. Here we make the application window contain a
single button which will shut down the application if clicked. The
code to make this happen is:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Tkx</span><span class="operator">;</span>
<span class="variable">Tkx::button</span><span class="operator">(</span><span class="string">".b"</span><span class="operator">,</span>
<span class="string">-text</span> <span class="operator">=></span> <span class="string">"Hello, world"</span><span class="operator">,</span>
<span class="string">-command</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="variable">Tkx::destroy</span><span class="operator">(</span><span class="string">"."</span><span class="operator">);</span> <span class="operator">},</span>
<span class="operator">);</span>
<span class="variable">Tkx::pack</span><span class="operator">(</span><span class="string">".b"</span><span class="operator">);</span>
<span class="variable">Tkx::MainLoop</span><span class="operator">()</span>
</pre>
<p>Save this to a file called <em>hello.pl</em> and then run <code>perl hello.pl</code>
to start up the application. A window with the text "Hello, world"
should appear on your screen.</p>
<p>After the Tkx module has been loaded by the <code>use Tkx</code> statement
the application will show an empty window called ".". We create a
<em>button</em> with the name ".b" and tell the window to display the button
with the call to <a href="../../lib/Pod/perlfunc.html#item_pack"><code>Tkx::pack()</code></a>. After the layout of the window has
been set up we need to pass control back to Tk so that it can draw
the window and invoke our callback if the button is clicked. This is
achieved by the <code>Tkx::MainLoop()</code> call at the end. Clicking the
button will invoke the subroutine registered with the <code>-command</code>
option of the button. In this case the callback simply destroys the
window, which in turn will terminate the application.</p>
<p>For reference this is how the same program would look in Tcl:</p>
<pre>
<span class="keyword">package</span> <span class="keyword">require</span> <span class="variable">Tk</span>
<span class="variable">button</span> <span class="operator">.</span><span class="variable">b</span> <span class="operator">\</span>
<span class="operator">-</span><span class="variable">text</span> <span class="string">"Hello, world"</span> <span class="operator">\</span>
<span class="operator">-</span><span class="variable">command</span> <span class="operator">{</span> <span class="variable">destroy</span> <span class="operator">.</span> <span class="operator">}</span>
<span class="keyword">pack</span> <span class="operator">.</span><span class="variable">b</span>
</pre>
<p>This program can be executed by the <em>tclsh</em> binary that comes with
Tcl/Tk. As you can see the code is mostly identical, but with a
slightly different syntax. The only real difference is that the call
to <code>MainLoop()</code> is implicit in Tcl and does not have to be spelled out.</p>
<p>Tkx does not come with documentation that explain all the widgets
(like "button" above) that are available for use. Instead you will
need to read the mostly excellent documentation that comes with Tcl/Tk and
then figure out how this translates to Tkx yourself. As you can
see this translation is straight forward. You basically only have add
the prefix "Tkx::" to all the functions and use the Perl way of
passing arguments. Tcl is a very simple language, so it should not
take long to understand enough of it to be able to read its
documentation with ease.</p>
<p>A great place to look up the Tk documentation is
<a href="http://aspn.activestate.com/ASPN/docs/ActiveTcl/at.pkg_index.html">http://aspn.activestate.com/ASPN/docs/ActiveTcl/at.pkg_index.html</a>.
This documents core Tk as well as all the useful add-on packages that
are part of ActiveTcl. The ActiveTcl HTML documentation can also be
downloaded from <a href="http://downloads.activestate.com/ActiveTcl/html/">http://downloads.activestate.com/ActiveTcl/html/</a>
and installed locally. The official Tcl/Tk docs are found at
<a href="http://www.tcl.tk/doc/">http://www.tcl.tk/doc/</a>.</p>
<p>
</p>
<h2><a name="hello_world_with_objects">Hello World with objects</a></h2>
<p>The windows and controls that make up a Tk interface are called
<em>widgets</em>. The widgets are identified by path names of the form
<code>.foo.bar.baz</code>. These names are hierarchical in the same way as file
system names are, but "." is used instead of "/" to separate levels.
The name <code>.foo.bar.baz</code> is the name of a widget that is child of widget
<code>.foo.bar</code> which in turn is a child of <code>.foo</code>. At the top of this
hierarchy we have a widget called <code>.</code>, which is the main window of
the application.</p>
<p>The Tkx module provide a class called <code>Tkx::widget</code>, that can be
used to hide the details of Tk path names from Tkx applications.
This provide a more "perlish" way to create and manipulate Tk widgets.</p>
<p>Our "Hello, world" program can be rewritten like this using the
<code>Tkx::widget</code> class:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Tkx</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$mw</span> <span class="operator">=</span> <span class="variable">Tkx::widget</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span><span class="string">"."</span><span class="operator">);</span>
<span class="keyword">my</span> <span class="variable">$b</span> <span class="operator">=</span> <span class="variable">$mw</span><span class="operator">-></span><span class="variable">new_button</span><span class="operator">(</span>
<span class="string">-text</span> <span class="operator">=></span> <span class="string">"Hello, world"</span><span class="operator">,</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?