Solved

Perl TK

Posted on 2004-08-16
4
726 Views
Last Modified: 2008-01-09
hi....

i would like to know whether perl tk can be connected to database (mysql)?
if so , can give me some sample codes for searching items from the db?
and i would want the data that is being searched to be display in the perl TK GUI

thankzzz
0
Comment
Question by:TPoly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 5

Expert Comment

by:ZiaTioN
ID: 11821913
Well the quick answer to your question is yes.

perl/Tk is simply a front end graphical interface used to give an application a nice point and click feel. Anything that perl can do via command line can also be done through Tk as a front end. You can use any of the database modules found at CPAN (http://search.cpan.org/search?query=DBI&mode=all) to accomplish the interaction between your script and your sql database. And yes anything being sent to the database and received from the database can be displayed in the Tk GUI (graphical user interface).

All you would have to do is capture the output from the command sent to the database and redirect it to be printed out to your Tk widget of choice (would have to be a text based widget to have text printed to it, ie. Tk::Text,, Tk::TextUndo, etc..). As far as example code goes for setting up the connection to the database and performaing all other backend functions of the script, there are many tutorials out there that explain how to manipulate any number of perl DBI modules. Here is a link (http://webmonkey.wired.com/webmonkey/98/24/index3a_page3.html?tw=backend) to one that gives a pretty decent explanation. You can go through the entire tutorial to get a better background and understanding on what is going on.
0
 
LVL 5

Accepted Solution

by:
ZiaTioN earned 200 total points
ID: 11822129
Here is a quick example of a Tk gui and an idea on how to redirect the database output to the Tk widget:

--------------------------code----------------------------

#!/usr/bin/perl -w
use Tk;
use DBI;
use strict;

our($filename, $username, $password, $info);

my $db = DBI->connect('DBI:mysql:test:localhost', '','') or die $DBI::errstr;

my $mw = MainWindow->new;
$mw->title("MySQL DataBase Script");

my $f = $mw->Frame->pack(-side => 'top', -fill => 'x');
$f->Label(-text => "Username:")->pack(-side => 'left', -anchor => 'w');
$f->Entry(-textvariable => \$username)->
   pack(-side => 'left', -anchor => 'w', -fill => 'x', -expand => 1);
$f->Label(-text => "Password:")->pack(-side => 'left', -anchor => 'w');
$f->Entry(-textvariable => \$password)->
   pack(-side => 'left', -anchor => 'w', -fill => 'x', -expand => 1);

$f->Button(-text => "Exit", -command => sub { exit; } )->
   pack(-side => 'right');
$f->Button(-text => "Disconnect", -command => \&disconnect)->
   pack(-side => 'right', -anchor => 'e');
$f->Button(-text => "Some Function", -command => \&function)->
   pack(-side => 'right', -anchor => 'e');
$mw->Label(-textvariable => \$info, -relief => 'ridge')->
   pack(-side => 'bottom', -fill => 'x');
my $t = $mw->Scrolled("Text")->pack(-side => 'bottom',
                     -fill => 'both', -expand => 1);

MainLoop;

sub disconnect {
   $db->disconnect();
}

sub function {
   my ($catalog, $schema, $table, $type);
   my $sth = $db->table_info( $catalog, $schema, $table, $type );
   $t->insert("end", $sth);
}

------------------------end code-----------------------------
0
 

Author Comment

by:TPoly
ID: 11827286
eh.. can u explain the codes

thanks
0
 
LVL 5

Expert Comment

by:ZiaTioN
ID: 11831604
Well the majority of it is the GUI. Read up on Tk widgets at CPAN and you will be able to read the code fairly easy. I know at first glance perl/Tk code looks a little jumbled and even overwhelming but it is really fairly easy. Basically "my $mw = MainWindow->new;" just creates the main window and then each widget below that simply builds on this main window. Most the Frame and Label widgets arguments are simply placement or geometric arguments. They tell the application where to place that specific widget with respect to the main window.

"my $db = DBI->connect('DBI:mysql:test:localhost', '','') or die $DBI::errstr;" is just an example of how to create the connection to your database. and then whenever the variable "$db" is used after that it is refering to the connection to the database so $db->disconnect() would disconnect you from your database. At the end of the subroutine "function" you can see the "$t->insert("end", $sth);", this is how you print to a text widget. In this case the text widget is represented by the variable "$t". And you can see we print the value of "$sth" to the text widget. This variable gets it's value from the output or response to the command "$db->table_info( $catalog, $schema, $table, $type );" which would be the info of the specific called table in your database.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question