Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6598510
  • 博文数量: 227
  • 博客积分: 10047
  • 博客等级: 上将
  • 技术积分: 6678
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-11 10:33
个人简介

网上的蜘蛛

文章分类

全部博文(227)

文章存档

2010年(19)

2009年(29)

2008年(179)

分类:

2008-05-30 10:53:06

  前段时间面试的时候,考官给我留了一个题目:利用TCL/TK简单的实现一个员工信息管理系统,对员工的ID,姓名,性别以及工作部分进行管理。我不对整个系统的开发作介绍,如果你有兴趣的话,可以下载源码自己编译执行。下载见附录。本文只是针对开发过程的遇到的几个重要知识点做一些笔记,以便以后复习。
 
1.平台搭建
   关于Tcl/TK我使用的是官方的ActionTCL8.5.2.0.数据库是Mysql5.0。兑入如何安装这些软件,请自行上网搜索。
 
2.TCL连接数据库
   本系统实在Windows平台上开发的,使用的数据库是Mysql.所以就像java连接mysql一样,都需要一个connector.这里我们使用的是MysqlTCL.windows版本的下载地址为:
安装很简单,解压到ActionTCL安装目录下的lib文件夹下。比如: C:\Tcl\lib. 程序中要使用mysqltcl必须加载这些.dll的文件。即使用语句:package require mysqltcl。
   现在介绍程序中的连接数据库执行SQL语句。举例:实现员工信息查询Mysql.
 
 

package require mysqltcl
set mysql_handler [mysqlconnect -user root -db test -password liceven]
mysqluse $mysql_handler test
#check each entry is null or not
    if {$eid !="" && $ename !="" && $egender != "" && $edep !=""} {
        mysql::sel $mysql_handler "select * from employee where id=\"$eid\""        
        #check duplicate key
        if {[llength [set row [mysql::fetch $mysql_handler]]]>0} {
         tk_messageBox -message "You input a duplicate ID!" -type ok -icon warning
        } else {
         catch {
                 mysqlexec $mysql_handler "insert into employee (id,name,gender,department) values(\"$eid\",\"$ename\",\"$egender\",\"$edep\")"    
         }
         tk_messageBox -message "Insert \"$ename\" into db successfully!" -type ok -icon info
             #clear the entries
                set form(Id) ""
                set form(Name) ""
                set form(Gender) ""
                set form(Department) ""
         }            
    } else {
        # pop up a warning message
        tk_messageBox -message "Fill all entries please!" -type ok -icon warning
     }
    #close hardler
    #mysqlclose $mysql_handler

  首先加载所有需要连接数据库的包。

  其次设定连接数据库的用户名密码以及使用的数据库名称。所有的这些信息保存在mysql_handler变量中。

  然后就是执行SQL语句了。以上程序中我们使用两种不同方法执行SQL语句。首先检查数据库是否有重复的id.执行语句是:

mysql::sel $mysql_handler "select * from employee where id=\"$eid\""

 我们可以根据

if {[llength [set row [mysql::fetch $mysql_handler]]]>0} {..
}

来判断没有重复的ID然后如果没有就插入新的员工信息:

mysqlexec $mysql_handler "insert into employee (id,name,gender,department) values(\"$eid\",\"$ename\",\"$egender\",\"$edep\")"    

这样就完成了查询和插入的操作了。同样要执行删除动作都是类似的操作。这里就不介绍了。

3.界面导航设计

    这个主要是考虑到界面的可操作性以及美观。因为功能其实跟界面其他组件的操作有重复,重要的是可以放入一个About,当用户点击这个选项的时候弹出作者的信息,这个我想应该是我设计的主要目的吧。

 

# create the menebar for the frame
. config -bd 3 -relief flat
frame .f -bg white
pack .f -fill both -expand 1
frame .f.mb -bd 2 -relief raised
pack .f.mb -side top -fill x
menubutton .f.mb.file -text File -menu .f.mb.file.menu
catch {
  menu .f.mb.file.menu
  .f.mb.file.menu add command -label Quit -command {
     #close mysql handler
     mysqlclose $mysql_handler
     exit
     }
 }
menubutton .f.mb.operation -text Operation -menu .f.mb.operation.menu
catch {
  menu .f.mb.operation.menu
  .f.mb.operation.menu add command -label New -command {Notebook:raise .n New}
  .f.mb.operation.menu add command -label Modify -command {Notebook:raise .n Modify}
  .f.mb.operation.menu add command -label Delete -command {Notebook:raise .n Delete}
}
menubutton .f.mb.about -text About -menu .f.mb.about.menu
catch {
  menu .f.mb.about.menu
  .f.mb.about.menu add command -label "About Me" -command \
  { tk_messageBox -message \
   "Author: YLFC\nAppling Position:Programmer\nEmail:feixianyexin‘AT’163.com\nVersion:1.0\nDate:2008-05-29" -type ok -icon info}
}
pack .f.mb.file .f.mb.operation .f.mb.about -side left -padx 10
#end of menubar

  代码我就不介绍了。跟其他语言都有相通之处。

4. ListBox的触发事件

  程序中使用ListBox作为让用户选择要删除员工的ID的一个界面组件。首先介绍插入从数据库读取的员工ID;

#add a listbox to the frame
listbox $w.f.lb -selectmode single -yscrollcommand "$w.f.sb set"
   #set m [mysqlconnect -user root -db test -password liceven]
   #mysqluse $m test
   mysql::sel $mysql_handler {select id from employee}
   #initialize the listbox
   while {[llength [set row [mysql::fetch $mysql_handler]]]>0} {
       set c [lindex $row 0]
       $w.f.lb insert end $c
    }
  #mysqlclose $mysql_handler
scrollbar $w.f.sb -orient vertical -command "$w.f.lb yview"

  使用insert方法,其实还包含了一个scrollbar.接着是事件触发,我们还需要一个按钮,当用户选择了list上面的某项,然后按下按钮执行操作:

 

button $w.f.b -text {Delete} -padx 10 -pady 5 -command [format {
    set t [%s cursel]
    if {[string length $t]>0} {
        set idno [%s get $t]
     #set mysql_handler [mysqlconnect -user root -db test -password liceven]
        #mysqluse $mysql_handler test
     #pup up a dialog to user to make sure to delete it
     set answer [tk_messageBox -message "Do you want to\n delete it?" -type yesno -icon warning]
     case $answer {
         yes {mysqlexec $mysql_handler "delete from employee where id =\"$idno\""}
         no {puts "Do nothing!"}
         }
     #refresh the list after deletion
     #remove all the items first
       set s [$w.f.lb size]
       for {set q 0 } {$q<$s} {incr q } {
         $w.f.lb delete 0
     }
       #insert the new items from db
     mysql::sel $mysql_handler {select id from employee}
       while {[llength [set row [mysql::fetch $mysql_handler]]]>0} {
            set c1 [lindex $row 0]
            $w.f.lb insert end $c1
       }
     #mysqlclose $mysql_handler    
    }

  拿到用户选择的那项id,然后执行删除操作,最后实现刷新功能,其实是先全部删除,然后重新加载最新的资料。代码中写的很清楚了,不介绍了。

5.关于消息对话框

   就是用来提示用户操作正确或者错误的信息。简单举个例子:

set answer [tk_messageBox -message "Insert Ok" -type ok -icon info]
case $answer {
   ok exit
   no {tk_messageBox -message "I know you like this application!" -type ok}
}

用户选择ok,退出程序,否则还是弹出一个对话框:I know you like this application。

就写到这里。源代码以及doc文档下载:

文件: Tcl-downloand.rar
大小: 376KB
下载:

阅读(2473) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~