• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 751
  • Last Modified:

Perl TK

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
TPoly
Asked:
TPoly
  • 3
1 Solution
 
ZiaTioNCommented:
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
 
ZiaTioNCommented:
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
 
TPolyAuthor Commented:
eh.. can u explain the codes

thanks
0
 
ZiaTioNCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now