环境及版本说明:

OSX10.9

tclsh -> tclsh8.5

wish -> wish8.5

查看本机运行环境:

  1. which wish;
  2. 2 /usr/bin/wish
  1. which tclsh;
  2. /usr/bin/tclsh

Demo功能说明:

用户登录窗口,输入用户名,密码.与文件中存储内容校验,如果相等,则提示"登录成功",否则提示"是否需要新建用户",点击"否"退出messageBox,点击"是"新建用户.内容追加写入文件.

  1. #!/usr/bin/wish -f
  2. #
  3. # this is test login, Tk-style
  4.  
  5. set filename "usrfile.pwd";
  6.  
  7. proc splitf { str index } {
  8. set y [ split $str | ];
  9. list $y;
  10. set w [ lindex $y $index ];
  11. return $w;
  12. }
  13.  
  14. proc checkinfo {name pwd} {
  15. global filename;
  16. set iflag ;
  17. set isUser ;
  18.  
  19. if { $name eq "" || $pwd eq "" } {
  20. return ;
  21. }
  22.  
  23. set fileId [ open $filename r ];
  24. while { [ gets $fileId line ] >= } {
  25. set nameInfile [ splitf $line ];
  26. set cmp [ string compare $name $nameInfile ];
  27.  
  28. if { $cmp != } {
  29. set isUser [ expr $isUser + ];
  30. } elseif { $cmp == } {
  31. set pwdInfile [ splitf $line ];
  32. set cmp [ string compare $pwd $pwdInfile ];
  33. if { $cmp == } {
  34. close $fileId;
  35. return ; #login successful
  36. } else {
  37. close $fileId;
  38. return ; #err user pwd
  39. }
  40. }
  41. set iflag [ expr $iflag + ];
  42. }
  43. close $fileId;
  44.  
  45. if { $iflag == } {
  46. return ; #file is null,creat user;
  47. }
  48. if { $iflag == $isUser } {
  49. return ; #creat user
  50. }
  51. }
  52.  
  53. proc process { uname pwd } {
  54. global filename;
  55. set returnCheck [ checkinfo $uname $pwd ];
  56. switch -- $returnCheck {
  57. { tk_messageBox -type ok -message "you are login successful" }
  58. { set answer [ tk_messageBox -type yesno -icon question \
  59. -message "you need creat a user" ] ;
  60. switch -- $answer {
  61. no { }
  62. yes { puts stdout [ createusr $uname $pwd ] }
  63. }
  64. }
  65. //003 { tk_messageBox -type ok -icon warning -message "$filename file is null" }
  66. { tk_messageBox -type ok -icon error -message "input err of user pwd" }
  67. { tk_messageBox -type ok -icon info -message "user and pwd is not null" }
  68. default { tk_messageBox -type ok -icon error -message "system err" }
  69. }
  70. }
  71.  
  72. proc createusr { uname pwd } {
  73. global filename;
  74. set fileId [ open $filename a ];
  75. puts $fileId "$uname|$pwd" ;
  76. close $fileId;
  77. return ;
  78. }
  79. wm title . LOGIN
  80. wm maxsize .
  81. wm minsize .
  82. wm resizable .
  83. wm geometry . 500x300++
  84.  
  85. label .ulname -text "userName"
  86.  
  87. entry .tuname -textvariable name
  88.  
  89. label .ulpwd -text "userPwd"
  90.  
  91. entry .tupwd -textvariable pwd
  92.  
  93. button .bOK -text OK \
  94. -command { puts stdout [ process $name $pwd] }
  95.  
  96. button .bExit -text Exit \
  97. -command {exit}
  98.  
  99. place .ulname -x -y
  100. place .tuname -x -y
  101. place .ulpwd -x -y
  102. place .tupwd -x -y
  103.  
  104. place .bOK -x -y
  105. place .bExit -x -y
  1. wish tk.tcl

配置文件格式:

  1. cat usrfile.pwd
    2 userTmp|abc123

此Demo涉及控件,语法.足够日常使用.望对新学者有点帮助.

特别需要注意:

1- 所有的关键字与{ 等之间一定要有空格,否则无法解析.

  1. 错误: proc sum{arg1 arg2}{
  1. 正确: proc sum { arg1 arg2 } {

2- proc if switch等需要用{}包含的body 左括号一定要写到 if {} { 与关键字同行

  1. proc sum { arg1 arg2 }
  2.  
  3. {
  4.  
  5.   ....
  6.  
  7. }

以上代码将提示:

Error in startup script: wrong # args: should be "proc name args body"

while executing

"proc sum {arg1 arg2} "

(file "a.tcl" line 3)

需要修改为:

  1. proc sum { arg1 arg2 } {
  2.  
  3.   ....
  4.  
  5. }

学习网站:

http://www.tcl.tk/man/tcl8.5/TclCmd/contents.htm

http://www2.tcl.tk/1062

http://blog.csdn.net/dulixin/article/category/366323

read-only access to git repository:

  1. git clone https://github.com/galoishelley/tcltk

tcl/tk demo的更多相关文章

  1. Tcl Tk Introduction

    Tcl Tk Introduction eryar@163.com 摘要Abstract:Tcl/Tck脚本可以很容易实现用户自定义的命令,方便的创建图形化的用户界面GUI,所以Tcl和Tk的应用领域 ...

  2. python INFO: Can't locate Tcl/Tk libs and/or headers

    安装opencv的时候遇到这个错误: python INFO: Can't locate Tcl/Tk libs and/or headers 参考如下文章解决这个问题: http://www.ver ...

  3. freewrap——将tcl/tk脚本转变为可执行文件

     FreeWrap可以把TCL/TK的脚本和二进制文件打包成应用程序,FreeWrap将所有的文件组合成一个单独的可执行文件.     FreeWrap的原理是把脚本和tcl/tk解释器和库文件都打包 ...

  4. 用Tcl/Tk脚本计算圆周率

      读了阮一峰的蒙特卡罗方法入门,用概率统计的方式求解棘手的数学问题还挺有意思的,尤其是利用正方形和它的内切圆之间的面积关系来建模求解圆周率的方法精巧又简单,比投针实验好理解也好实现多了.建模可不是M ...

  5. Tcl/tk缩放Truetype字体时的精度问题

    最近有国内新客户抱怨我们产品显示的原理图太不专业了,在原理图上使用宋体GB2312设计好中文图表,经过几次缩放时,表格内的文字居然会跑到表格外边,更要命的是打印出来的文档也存在同样的问题. 我研究了一 ...

  6. linux下ruby使用tcl/tk编程环境设置

    正常情况下最新的ruby都是不带tcl/tk选项编译的,所以我们在运行tcl/tk代码时都会发生找不到tk库的错误.解决办法很简单只要以tcl/tk选项编译ruby即可. 这里以ubuntu 15.0 ...

  7. Mac OS X下让ruby支持tcl/tk

    我记得在老早在OS X10.8下使用ruby1.9.x的时候只要到下载安装ActiveTcl8.5,没怎么配置就运行tk好好的.但是近日想重新执行下tk代码,发现在require 'tk'的时候就报错 ...

  8. pgtksh -- PostgreSQL Tcl/Tk shell 客户端

    SYNOPSIS pgtksh [filename [argument...]] DESCRIPTION 描述 pgtksh 是一个带有 PostgreSQL 数据库访问函数扩展的 Tcl/Tk sh ...

  9. tcl/tk实例详解——glob使用例解

    glob命令 这里以实例的形式解释一下glob命令的用法,很多时候纯粹的语法讲解根本讲不清楚,往往没有一个例子清晰,一下就glob命令进行一些分析,环境为Tclsh85,操作系统为windows XP ...

随机推荐

  1. phpexcel导入excel文件报the filename xxx is not recognised as an OLE file错误。

    工作中频繁会用phpexcel类导入excel文件的数据到数据库,目前常用的excel文件格式有:xls.csv.xlsx. 刚开始,针对xls文件,使用如下程序,能正常运行: $objReader ...

  2. 解决pip安装时 UnicodeDecodeError 问题

    在装django的时候用pip安装就出现了问题,一大堆的红字,然后联想到很多次用pip安装都以失败告终,于是今天抽空看了看出现问题的原因. 情况如下图:

  3. oc语言--面向对象的三大特性

    一.封装 1.什么是封装 在程序上,隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别:将对象得到的数据和行为(或功能)相结合,形成一个有机的整体,也就是将数据与操作数据的 ...

  4. MVC Controller 与 View 传值

    Controller 到 View 1 强类型 控制器 // GET: /Test/ public ActionResult Index() { DateTime date = DateTime.No ...

  5. Nginx限制模块研究

    已有方案:无非两种 1. 限制连接个数: 是指一台机多线程或多进程开启请求. 解决方案-ngx_http_limit_conn_module 缺点: 定义$variable为单机时,只能限制已有ngi ...

  6. HttpContext请求上下文对象

    一.HttpContext概述 HttpContext基于HttpApplication的处理管道,由于HttpContext对象贯穿整个处理过程,所以,可以从HttpApplication处理管道的 ...

  7. 认识IL代码---从开始到现在 <第二篇>

    ·IL代码分析方法 ·IL命令解析 ·.NET学习方法论 1.引言 自从『你必须知道.NET』系列开篇以来,受到大家很多的关注和支持,给予了anytao巨大的鼓励和动力.俱往昔,我发现很多的园友都把目 ...

  8. Windows7安装SQL Server 2008图解

    不知是什么时候,把这篇博客给删除了,今天才发现,想恢复好像又不行,所以重新发布一下吧! 这几天因为需要,一直想安装SQL Server 2008来作为Web后台的数据库进行些实验,但总是没有时间,今天 ...

  9. Linux系统编程(3)——文件与IO之fcntl函数

    linux文件I/O用:open.read.write.lseek以及close函数实现了文件的打开.读写等基本操作.fcntl函数可以根据文件描述词来操作文件. 用法: int fcntl(int ...

  10. cf472D Design Tutorial: Inverse the Problem

    D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test 256 mega ...