Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl TK Problem 1

Status
Not open for further replies.

Raenius

Programmer
Oct 14, 2003
77
NL
Hi all,

I just took my first steps to build a GUI in Perl fom my log analyser.

Now I want to create a standard topmenu with dropdown menus.

I have the following code, but it does not seem to display the second menu item I want to show. (first being file and next to it I want edit). When I change the variable to 'right' next to the pack method it does show only on the other side of the menu instead of next to it. :-S Does someone have suggestions?

Code:

# Test GUI

use Tk;

$main = MainWindow->new();

$menubar = $main->Frame( - relief => "raised",
- borderwidth => 2)
->pack ( - anchor => "nw",
- fill => "x");

$file_menu = $menubar->Menubutton( - text => "File",
- underline => 1,
- menuitems => [
[ Button => "Open", -command => \&Open ],
[ Button => "Save as", -command => \&SaveAs ]
]
)
-> pack( - side => "left");

$file_menu = $menubar->Menubutton( - text => "Edit",
- underline => 1,
- menuitems => [
[ Button => "Cut", -command => \&Print ],
[ Button => "Copy", -command => \&Save ]
]
);
#-> pack( - side => "left");

MainLoop;



"Free will...is an illusion"
 
Once you have packed your first menu button into the proper side, then you have to add "fill" to the rest of the buttons.

use Tk;

$main = MainWindow->new();

$menubar = $main->Frame( - relief => "raised",
- borderwidth => 2)
->pack ( - anchor => "nw",
- fill => "x");

$file_menu = $menubar->Menubutton( - text => "File",
- underline => 1,
- menuitems => [
[ Button => "Open", -command => \&Open ],
[ Button => "Save as", -command => \&SaveAs ]
]
)
-> pack( - side => "left");

$file_menu = $menubar->Menubutton( - text => "Edit",
- underline => 1,
- menuitems => [
[ Button => "Cut", -command => \&Print ],
[ Button => "Copy", -command => \&Save ]
]
)
-> pack( - side => "left", - fill => "x"); # A fill statement has been added

MainLoop;
 
Ahh great thanks a lot! That worked like a charm.

I just wrote a log analyser which I will incorparate using this GUI. Now I've got this working that should come along slowly but surely.

Do you by any chance happen to know how I can open a file via the GUI (just I as you would do when you open an other file in e.g. notepad. (that you can browse through your HD)

I am building this on Windows XP with Activestate5.8

Thanks for the help so far!

- Raenius...

"Free will...is an illusion"
 
Wander around CPAN, here's a couple:

I remember in VB, I just called something in the WinAPI or an ActiveX control. If it was the former, you could probably do the same with some Win32:: module, but I've not done it.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Thanks to both of you. I will look into those things and post back here if it works ;-)

- Raenius

"Free will...is an illusion"
 
Tk::FileDialog can do what you want - it's a bit tricky to get the hang of it but it works fine for me on NT / Linux.
 
I am continuing on my way to build a nicve (working) gui and I encountered a problem with a DialogBox:

D:\Perl\Homework\Perl Scripting\Handy>gui.pl
Tk::Error: Can't locate auto/Tk/DialogBox.al in @INC (@INC contains: D:/Perl/lib
D:/Perl/site/lib .) at D:\Perl\Homework\Perl Scripting\Handy\gui.pl line 94
[\&main::eek:nAbout]
(menu invoke)

This is the sub it is calling:

# Show the Help->About Dialog Box
sub onAbout {
# Construct the DialogBox
my $about = $main->Tk::DialogBox(
-title=>"About Jack",
-buttons=>["OK"]
);

# Now we need to add a Label widget so we can show some text. The
# DialogBox is essentially an empty frame with no widgets in it.
# You can images, buttons, text widgets, listboxes, etc.
$about->add('Label',
-anchor => 'w',
-justify => 'left',
-text => qq(
Perl Eval-uator v1.0 by David Hisel

-Click on a filename to view it, and if it has a
".pl" suffix, it will be evaluated automatically, or
-Copy and paste Perl code to the top window, then
-Hit CTRL-L to evaluate the code and
display the output in the bottom text widget.
)
)->pack;

$about->Show();
}

It says it cannot locate a Dialogbox.al what is that? And where can I get it?

Thanks in advance..

- Raenius


"Free will...is an illusion"
 
Is Tk::Dialogbox installed on your system? Did you include a "use" statement (use Tk::Dialogbox,) in your script?
 
Yes the use command is in place: use DialogBox;

And Tk is installed on my system, when I search in my Tk directory I find DialogBox.pm and DialogBox.pod but no DialogBox.al what is that?



"Free will...is an illusion"
 
Here you go:

# Test GUI
use strict;

use Tk;
use Tk::Frame;
use Tk::TextUndo;
use Tk::Text;
use Tk::Scrollbar;
use Tk::Menu;
use Tk::Menubutton;
use Tk::Adjuster;
use Tk::Dialogbox;

my $main;
my $menubar;
my $file_menu;

$main = MainWindow->new();
$main->geometry('400x300');

$main->title( "DCP Log Analyser");

$main->Label( - text=>"Version 1.1 by Patrick de Kok") # Create label and pack bottom
->pack ( - side => 'bottom',
- fill => "x");

$menubar = $main->Frame( - relief => "raised",
- borderwidth => 2)
->pack ( - anchor => "nw",
- fill => "x");

$file_menu = $menubar->Menubutton( - text => "File",
- underline => 1,
- menuitems => [
[ Button => "Open", -command => \&onFileOpen ],
[ Button => "Save as..", -command => \&SaveFile ],
[ Button => "Quit", -command => \&onQuit ]
]
)
-> pack( - side => "left");

$file_menu = $menubar->Menubutton( - text => "Options",
- underline => 1,
- menuitems => [
[ Button => "Configure", -command => \&Print ],
]
)
-> pack( - side => "left");
#- fill => "x");

$file_menu = $menubar->Menubutton( - text => "Help",
- underline => 1,
- menuitems => [
[ Button => "About", -command => \&onAbout ],
[ Button => "Helpfile", -command => \&Print ],
]
)
-> pack( - side => "left",
- fill => "x");
# toying with frames
my $lf = $main->Frame; # Left Frame;
my $aj = $main->Adjuster(-widget => $lf, -side => 'left'); # So we can scroll vertically
my $rf = $main->Frame; # Right Frame;

$lf->pack(qw/-side left -fill y/);
$aj->pack(qw/-side left -fill y/);
$rf->pack(qw/-side right -fill both -expand 1/);

my($InputText) = $rf->Frame;




MainLoop;

# Functions


sub onQuit
{
exit;
}

# The TextUndo widget has a file load dialog box method built-in!
sub onFileOpen {
my $InputText->FileLoadPopup();
}


# Show the Help->About Dialog Box
sub onAbout {
# Construct the DialogBox
my $about = $main->Tk::DialogBox(
-title=>"About Jack",
-buttons=>["OK"]
);

# Now we need to add a Label widget so we can show some text. The
# DialogBox is essentially an empty frame with no widgets in it.
# You can images, buttons, text widgets, listboxes, etc.
$about->add('Label',
-anchor => 'w',
-justify => 'left',
-text => qq(
Perl Eval-uator v1.0 by David Hisel

-Click on a filename to view it, and if it has a
".pl" suffix, it will be evaluated automatically, or
-Copy and paste Perl code to the top window, then
-Hit CTRL-L to evaluate the code and
display the output in the bottom text widget.
)
)->pack;

$about->Show();
}



"Free will...is an illusion"
 
Strange. I ran your code (no changes made to it) and it popped up a gui window and no error messages.

Here is a quote from the Tk::Dialogbox doc:

NAME

Tk::DialogBox - create and manipulate a dialog screen.

This module is not included with the standard ActivePerl distribution. It is available as a separate download using PPM.

What is even more strange though is that I did not download and install Tk::Dialogbox. I did look for it on PPM and did install something that I thought was it, but it turned out to be Tk::XDialogbox. I uninstalled it, but maybe it left something behind that I needed? I did a search for Dialogbox.al and I don't have that anywhere either.
 
try changing this line -

my $about = $main->Tk::DialogBox(

to

my $about = $main->DialogBox(


it worked for me!
 
I just tried it on my home computer with the changed line Chazoid suggested and it worked perfectly! ;-)

I only downloaded ActiveState here and nothing else on my Windows XP machine and it worked liek a charm.

I haven't been able to make the other stuff work to load a text file or something but I am sure that will work out.

If not, I am sure I will be back here soon.

Thanks for all your help..I owe you a lot.

- Raenius

"Free will...is an illusion"
 
Strange thing, it stil does not work on my home pc, this time it says that popup.pm is not installed on my system.. :(

"Free will...is an illusion"
 
Correction: Stil does not work on my WORK pc. At home it works (where I did not install any modules :-S)

"Free will...is an illusion"
 
Are you running an older version of ActivePerl at work? Strange thing is, I couldn't find popup.pm in my perl directory either, but I don't get that error message. When do you get the error, when you select Help > About?
(I'm using version 5.6.1)
 
Yes when I select Help -> About it gives me the error.

I am allmost sure that the versions of ActiveState are the same.

"Free will...is an illusion"
 
I checked last night and both versions are the same. I still don't have a clue why it does not work :(

"Free will...is an illusion"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top