G'day saif,
Welcome to the Monastery.
The code you've posted works fine.
If that's intended as an example of what the GUI should ultimately look like, that's great; however,
it doesn't tell us what problems you're encountering with your actual development using
Tk::place.
Please provide another script — that works poorly; or perhaps not at all —
so that we know how we can help.
Now, although that code works as is, it does have some problems.
In addition, some of your assumptions seem questionable.
Given "I am learning Perl and GUI development', that's perfectly understandable.
Please go through the following list and apply the various points to your new script.
-
Using strict is good; not using warnings is bad.
See "Safety net", saiftynet :-)
-
In Tk::Listbox' description,
you'll see: "A listbox is a widget that displays a list of strings, one per line.".
If you add something other than a string, it will be stringified; no warning is emitted.
-
For example, if you add a Tk::Frame object, you'll get a tcl/Tk path (.frame);
an undef will render as a blank entry in the list;
\*STDERR will appear as something like GLOB(0xhhhhhhhhh); and so on.
Arrayrefs are a bit of a curiosity rendering what I believe is some sort of tcl array format:
[1,2,3] becomes 1 2 3; [1,[2,3,[4,5,6],7,8],9] becomes 1 {2 3 {4 5 6} 7 8} 9.
Have a play around with that if you're interested.
-
Have a look in Tk::DItem.
There's links to a number of list widgets that are probably more suitable for your needs.
Also see the Widget Demo for working examples and full code.
-
$canvas->{"listbox"} is a huge no-no!
-
You are taking a Tk::Canvas object:
$ perl -E 'use Tk; my $mw = MainWindow::->new(); say $mw->Canvas'
Tk::Canvas=HASH(0x8003cb500)
You then create a new, or perhaps even overwrite an existing, "listbox" key
which you populate with a Tk::Frame object;
yes, that's correct, not a Tk::Listbox object as your naming suggests:
$ perl -E 'use Tk; my $mw = MainWindow::->new(); say $mw->Listbox'
Tk::Listbox=HASH(0x8006859e8)
$ perl -E 'use Tk; my $mw = MainWindow::->new(); say $mw->Scrolled("Li
+stbox")'
Tk::Frame=HASH(0x800647820)
-
You have broken encapsulation and are dealing directly with the internal implementation of the object.
You should only ever use an object's published interface!
This is fundamental and applies to all objects.
At anytime, the author of any class can change the implementation without notice —
the object type could change; the object's internal structure could be rearranged;
invalid or unknown keys might be suppressed; the key or index you thought was unused may be added later —
while still keeping the published interface.
-
It would have been simpler, less work, and not error-prone to have written something like:
my $scrl_listbox = $mw->Scrolled("Listbox", ...);
and then just used $scrl_listbox everywhere you currently have $canvas->{"listbox"}.
-
My apologies if that section came across as an angry rebuke.
I know you're learning and that was not my intent.
It is, however, important that you understand that you don't interfere with the internal implementation,
and only access it through the published interface.
-
You don't need to write (${$varname}[$index]).
It's verbose and doesn't help readability.
That would have been much better simply as $varname->[$index].
The same would apply to other code like (${$varname}{$key}) vs. $varname->{$key}.
-
Hard-coding values is generally a bad move, especially when those values appear more than once.
If they need changing, will you remember to change them all?
If, for instance, you have 12 in multiple places, and they have a variety of purposes
— indices, sizes, durations, and so on —
how easy would it be to identify which 12s to change?
If you see $some_array[12], how easy will it be for you to remember what value is held by the 13th index?
Why would you want to make this rod for your back?
-
A better course of action would be to give them names and only store the value once.
There are a number of ways to do this,
hashes and the constant pragma are often good choices.
-
The title has "... Scrolling Pane".
Have you seen Tk::Pane?
-
You can use more than one geometry manager in a single Tk application; in fact, it's often quite useful.
Your general layout might have frames managed by Tk::pack and,
within some of those, you might have elements managed by Tk::grid.
-
You mentioned mixing Tk::pack
and Tk::place,
but gave no indication of what you were planning, so I can't really comment further on that.
Your new script should indicate how you were going to mix those two.
Overall, for someone just learning, that's a pretty good effort.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.