tkx.html
来自「perl教程」· HTML 代码 · 共 465 行 · 第 1/2 页
HTML
465 行
<dd>
<p>Returns a hash that can be used to keep instance specific data. This
is useful for holding instance data for megawidgets. The data is
attached to the underlying widget, so if you create another handle to
the same widget it will return the same hash via its <a href="#item__data"><code>_data()</code></a> method.</p>
</dd>
<dd>
<p>The data hash is automatically destroyed when the corresponding widget
is destroyed.</p>
</dd>
</li>
<dt><strong><a name="item__parent">$w->_parent</a></strong>
<dd>
<p>Returns a handle for the parent widget. Returns <a href="../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> if there is
no parent, which will only happen if $w is ".", the main window.</p>
</dd>
</li>
<dt><strong><a name="item__kid">$w->_kid( $name )</a></strong>
<dd>
<p>Returns a handle for a kid widget with the given name. The $name can
contain dots to access grandkids. There is no check that a kid with
the given name actually exists, so this method can't fail. This is a
feature. It can for instance be used to construct names of widgets to
be created later.</p>
</dd>
</li>
<dt><strong><a name="item__class">$w->_class( $class )</a></strong>
<dd>
<p>Sets the widget handle class for the current path. This will both
change the class of the current handle and make sure later handles
created for the path belong to the given class. The class should
normally be a subclass of <code>Tkx::widget</code>. Overriding the class for a
path is useful for implementing megawidgets. Kids of $w are not
affected by this, unless the class overrides the <a href="#item__nclass"><code>_nclass</code></a> method.</p>
</dd>
</li>
<dt><strong><a name="item__nclass">$w->_nclass</a></strong>
<dd>
<p>This returns the default widget handle class that will be used for
kids and parent. Subclasses might want to override this method.
The default implementation always returns <code>Tkx::widget</code>.</p>
</dd>
</li>
<dt><strong><a name="item__mpath">$w->_mpath( $method )</a></strong>
<dd>
<p>This returns a Tcl widget path that will be used to forward any
m_<em>foo</em> method calls. Megawidget classes might want to override
this method. The default implementation returns <code>$w</code>.</p>
</dd>
</li>
<dt><strong><a name="item_new_foo">$new_w = $w->new_<em>foo</em>( @args )</a></strong>
<dd>
<p>This creates a new <em>foo</em> widget as a child of the current widget. It
will call the <em>foo</em> Tcl command and pass it a new unique subpath of
the current path. The handle to the new widget is returned. Any
double underscores in the name <em>foo</em> is expanded as described for
Tkx::foo() above.</p>
</dd>
<dd>
<p>Example:</p>
</dd>
<dd>
<pre>
<span class="variable">$w</span><span class="operator">-></span><span class="variable">new_label</span><span class="operator">(</span><span class="string">-text</span> <span class="operator">=></span> <span class="string">"Hello"</span><span class="operator">,</span> <span class="string">-relief</span> <span class="operator">=></span> <span class="string">"sunken"</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>The name selected for the child will be the first letter in the
widget. If that name is not unique a number is appended to ensure
uniqueness among the children. If a <code>-name</code> argument is passed it is
used to form the name and then removed from the arglist passed to Tcl.
Example:</p>
</dd>
<dd>
<pre>
<span class="variable">$w</span><span class="operator">-></span><span class="variable">new_iwidgets__calendar</span><span class="operator">(</span><span class="string">-name</span> <span class="operator">=></span> <span class="string">"cal"</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>If a megawidget implementation class has be registered for <em>foo</em>,
then its <code>_Populate</code> method is called instead of passing widget
creation to Tcl.</p>
</dd>
</li>
<dt><strong><a name="item_m_foo">$w->m_<em>foo</em>( @args )</a></strong>
<dd>
<p>This will invoke the <em>foo</em> subcommand for the current widget. This
is the same as:</p>
</dd>
<dd>
<pre>
<span class="variable">$func</span> <span class="operator">=</span> <span class="string">"Tkx::$w"</span><span class="operator">;</span>
<span class="operator">&</span><span class="variable">$func</span><span class="operator">(</span><span class="variable">expand</span><span class="operator">(</span><span class="string">"foo"</span><span class="operator">),</span> <span class="variable">@args</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>where the <code>expand()</code> function expands underscores as described for
Tkx::foo() above. Note that methods that do not start with a prefix
of the form /^_/ or /^[a-zA-Z]_/ are also treated as the <code>m_</code> methods.</p>
</dd>
<dd>
<p>Example:</p>
</dd>
<dd>
<pre>
<span class="variable">$w</span><span class="operator">-></span><span class="variable">m_configure</span><span class="operator">(</span><span class="string">-background</span> <span class="operator">=></span> <span class="string">"red"</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Subclasses might override the <a href="#item__mpath"><code>_mpath()</code></a> method to have m_<em>foo</em> forward
the subcommand somewhere else than the current widget.</p>
</dd>
</li>
<dt><strong><a name="item_g_foo">$w->g_<em>foo</em>( @args )</a></strong>
<dd>
<p>This will invoke the <em>foo</em> Tcl command with the current widget as
first argument. This is the same as:</p>
</dd>
<dd>
<pre>
<span class="variable">$func</span> <span class="operator">=</span> <span class="string">"Tkx::foo"</span><span class="operator">;</span>
<span class="operator">&</span><span class="variable">$func</span><span class="operator">(</span><span class="variable">$w</span><span class="operator">,</span> <span class="variable">@args</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Any underscores in the name <em>foo</em> are expanded as described for
Tkx::foo() above.</p>
</dd>
<dd>
<p>Example:</p>
</dd>
<dd>
<pre>
<span class="variable">$w</span><span class="operator">-></span><span class="variable">g_pack_forget</span><span class="operator">;</span>
</pre>
</dd>
</li>
<dt><strong>$w-><em>foo</em>( @args )</strong>
<dd>
<p>If the method does not start with "new_" or have a prefix of the form
/^_/ or /^[a-zA-Z]_/, then it is treated as if it had the "m_" prefix,
i.e. the <em>foo</em> subcommand for the current widget is invoked.</p>
</dd>
<dd>
<p>The method names with prefix /^_/ and /^[a-zA-Z]_/ are reserved for
future extensions to this API.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="megawidgets">Megawidgets</a></h2>
<p>Megawidgets can be implemented in Perl and used by Tkx. To declare a
megawidget make a Perl class like this one:</p>
<pre>
<span class="keyword">package</span> <span class="variable">Foo</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">base</span> <span class="string">'Tkx::widget'</span><span class="operator">;</span>
<span class="variable">Foo</span><span class="operator">-></span><span class="variable">_Mega</span><span class="operator">(</span><span class="string">"foo"</span><span class="operator">);</span>
</pre>
<pre>
<span class="keyword">sub</span><span class="variable"> _Populate </span><span class="operator">{</span>
<span class="keyword">my</span><span class="operator">(</span><span class="variable">$class</span><span class="operator">,</span> <span class="variable">$widget</span><span class="operator">,</span> <span class="variable">$path</span><span class="operator">,</span> <span class="variable">%opt</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
<span class="operator">...</span>
<span class="operator">}</span>
</pre>
<p>The megawidget class should inherit from <code>Tkx::widget</code> and will
register itself by calling the <a href="#item__mega"><code>_Mega()</code></a> class method. In the example
above we tell Tkx that any "foo" widgets should be handled by the Perl
class "Foo" instead of Tcl. When a new "foo" widget is instantiated
with:</p>
<pre>
<span class="variable">$w</span><span class="operator">-></span><span class="variable">new_foo</span><span class="operator">(</span><span class="string">-text</span> <span class="operator">=></span> <span class="string">"Hi"</span><span class="operator">,</span> <span class="string">-foo</span> <span class="operator">=></span> <span class="number">1</span><span class="operator">);</span>
</pre>
<p>then the <code>_Populate()</code> class method of <code>Foo</code> is called. It will be
passed the widget type to create, the full path to use as widget
name and any options passed in. The widget name is passed in so that a
single Perl class can implement multiple widget types.</p>
<p>The <code>_Populate()</code> class should create a root object with the given $path
as name and populate it with the internal widgets. Normally the root
object will be forced to belong to the implementation class so that it
can trap various method calls on it. By using the <a href="#item__class"><code>_class()</code></a> method to
set class <code>_Populate()</code> can ensure that new handles to this megawidget
also use this class.</p>
<p>The implementation class can define an <code>_ipath()</code> method to delegate any
m_<em>foo</em> method calls to one of its subwidgets and it might want to
override the <code>m_configure()</code> and <code>m_cget()</code> methods if it implements
additional options or want more control over delegation. The class
<code>Tkx::MegaConfig</code> provide implementations of <code>m_configure()</code> and
<code>m_cget()</code> that can be useful for controlling delegation of
configuration options.</p>
<p>See <a href="../lib/Tkx/LabEntry.html">the Tkx::LabEntry manpage</a> for a trivial example megawidget.</p>
<p>
</p>
<hr />
<h1><a name="environment">ENVIRONMENT</a></h1>
<p>The <code>PERL_TKX_TRACE</code> environment variable initialize the $Tkx::TRACE setting.</p>
<p>
</p>
<hr />
<h1><a name="support">SUPPORT</a></h1>
<p>If you have questions about this code or want to report bugs send a
message to the <<a href="mailto:tcltk@perl.org">tcltk@perl.org</a>> mailing list. To subscribe to this
list send an empty message to <<a href="mailto:tcltk-subscribe@perl.org">tcltk-subscribe@perl.org</a>>.</p>
<p>
</p>
<hr />
<h1><a name="license">LICENSE</a></h1>
<p>This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.</p>
<p>Copyright 2005 ActiveState. All rights reserved.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../lib/Tkx/Tutorial.html">the Tkx::Tutorial manpage</a>, <a href="../lib/Tkx/MegaConfig.html">the Tkx::MegaConfig manpage</a>, <a href="../lib/Tcl.html">the Tcl manpage</a></p>
<p>Alternative Tk bindings for Perl are described in <a href="../Tcl/Tk.html">the Tcl::Tk manpage</a> and <a href="../lib/Tk.html">the Tk manpage</a>.</p>
<p>More information about Tcl/Tk can be found at <a href="http://www.tcl.tk/">http://www.tcl.tk/</a>.
Tk documentation is also available at <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>.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?