Solved

Perl TK

Posted on 2004-08-16
4
710 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now