tcl/tk demo

环境及版本说明:

OSX10.9 

tclsh -> tclsh8.5

wish -> wish8.5

查看本机运行环境:

1 which wish;
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 0;
 17     set isUser 0;
 18     
 19     if { $name eq "" || $pwd eq "" } {
 20         return 005;
 21     }
 22 
 23     set fileId [ open $filename r ];   
 24     while { [ gets $fileId line ] >= 0 } {
 25         set nameInfile [ splitf $line 0 ];
 26         set cmp [ string compare $name $nameInfile ];
 27 
 28         if { $cmp != 0 } {
 29             set isUser [ expr $isUser + 1 ];
 30         } elseif { $cmp == 0 } {
 31             set pwdInfile [ splitf $line 1 ];
 32             set cmp [ string compare $pwd $pwdInfile ];
 33             if { $cmp == 0 } {
 34                 close $fileId; 
 35                 return 001; #login successful 
 36             } else {
 37                 close $fileId;
 38                 return 004; #err user pwd
 39             } 
 40         }
 41         set iflag [ expr $iflag + 1 ];
 42     }
 43     close $fileId;
 44 
 45     if { $iflag == 0 } {
 46         return 002; #file is null,creat user;
 47     }
 48     if { $iflag == $isUser } {
 49         return 002; #creat user
 50     }
 51 }
 52 
 53 proc process { uname pwd } {
 54     global filename;
 55     set returnCheck [ checkinfo $uname $pwd ];
 56     switch -- $returnCheck {
 57         001 { tk_messageBox -type ok -message "you are login successful" }
 58         002 { 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         004 { tk_messageBox -type ok -icon error -message "input err of user pwd" }
 67         005 { 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 1; 
 78 }
 79 wm title . LOGIN
 80 wm maxsize . 500 300
 81 wm minsize . 500 300
 82 wm resizable . 500 300
 83 wm geometry . 500x300+300+200
 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 
100 place .ulname -x 110 -y 50 
101 place .tuname -x 200 -y 50 
102 place .ulpwd -x 110 -y 100 
103 place .tupwd -x 200 -y 100 
104 
105 place .bOK -x 150 -y 150  
106 place .bExit -x 280 -y 150
1 wish tk.tcl

配置文件格式:

1 cat usrfile.pwd
2 userTmp|abc123

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

特别需要注意:

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

错误: proc sum{arg1 arg2}{
正确: 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:

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

 

转载于:https://www.cnblogs.com/galoishelley/p/3411973.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/473839.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Hive高级操作

Hive高级操作 1 Hive的数据类型 1.1 原子数据类型 (1)Hive 是用 Java 开发的,Hive 里的基本数据类型和 java 的基本数据类型也是一一对应的, 除了 String 类型。 (2)有符号的整数类型:TINYIN…

05.序列模型 W2.自然语言处理与词嵌入(作业:词向量+Emoji表情生成)

文章目录作业1:1. 余弦相似度2. 单词类比3. 词向量纠偏3.1 消除对非性别词语的偏见3.2 性别词的均衡算法作业2:Emojify表情生成1. Baseline model: Emojifier-V11.1 数据集1.2 模型预览1.3 实现 Emojifier-V11.4 在训练集上测试2. Emojifier-V2: Using L…

4.搭建主从

一个master可以拥有多个slave,一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架构 比如,将ip为192.168.1.10的机器作为主服务器,将ip为192.168.1.11的机器作为从服务器 说明:ip可以换为…

android中给TextView或者Button的文字添加阴影效果

参考: http://blog.csdn.net/hahahacff/article/details/8653171 总结: 可以在代码中实现也可以在xml中实现; XML如下: android:shadowColor"color/black" android:shadowDx"0" android:shadowDy"1"…

LeetCode 688. “马”在棋盘上的概率(DP)

文章目录1. 题目2. 解题1. 题目 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始。即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1)。 现有一个 “马”(也译作 “骑士”)位于 (r, c) ,并打算进行…

5.搭建集群

1.配置机器1 在演示中,192.168.12.107为当前ubuntu机器的ip 在192.168.12.107上进入Desktop目录,创建redis目录 在redis目录下创建文件7000.conf,编辑内容如下 port 7000 bind 192.168.12.107 daemonize yes pidfile 7000.pid cluster-enabl…

知识管理系统

一 :概述知识管理中, 一般是设定学习目标, 有针对性的收集材料,然后学习更有效;另外如见到以前总结过的内容, 并对自己有启发的也可以保存, 以修改以前写过的文章;而学习目标是什么&…

LeetCode 第 36 场双周赛(304/2204,前13.8%)

文章目录1. 比赛结果2. 题目1. LeetCode 5515. 设计停车系统 easy2. LeetCode 5516. 警告一小时内使用相同员工卡大于等于三次的人 medium3. LeetCode 5518. 给定行和列的和求可行矩阵 medium4. LeetCode 5517. 找到处理最多请求的服务器 hard1. 比赛结果 做出来3题&#xff0…

数据库实例:用户注册

1.根据结构创建表的脚本如下 createtable py_users( id int unsigned auto_increment not null primary key, uname varchar(20) not null, upwd char(40) not null, is_delete bit not null default 0 ); 如下流程图,接下来的代码就按照这个逻辑来写 2.创建user…

android 应用在启动后进行全局的的初始化操作

例如对于Volley的使用: 1:定义一个类使其继承Application package com.wzh.app;import com.wzh.volley.WzhVolley; import android.app.Application; import android.util.Log;public class WzhShshtApp extends Application{Overridepublic void onCrea…

HBase原理

目录 HBase原理 1 HBase架构 2 HBase中的核心概念 3 HBase的存储机制 4 HBase的寻址机制 5 HBase的读写流程 6 HBase的设计 7 HBase和Hive的整合 HBase原理 1 HBase架构 HBase的架构为主从架构,HMaster为主节点,HRegionServer为从节点 &#x…

数据库实例:用户登录

如下遍流程图,接下来的代码就按照这个逻辑来写 创建user_login.py文件,代码如下 #codingutf-8 from MySQLdb import* from hashlib importsha1 if __name____main__: try: #接收输入用户名、密码 unameraw_input(请输入用户名&…

javascript自定义startWith()和endWith()方法

一、采用正则表达式实现startWith、endWith效果函数 String.prototype.startWithfunction(str){ var regnew RegExp("^"str); return reg.test(this); } //测试ok,直接使用str.endWith("abc")方式调用即可String.prototyp…

数据库实例:mysql与mongo结合用户登录

加入mongodb后登录逻辑如下图,将图中nosql的位置换为mongodb即可 用户数据存储的集合名称为py_users,文档格式为{uname:用户名,upwd:密码} 将原来MySQL操作的代码封装到一个方法中,代码如下 def mysql_login(): #mongodb中没有则到mysql中…

LeetCode 1609. 奇偶树(层序遍历)

文章目录1. 题目2. 解题1. 题目 如果一棵二叉树满足下述几个条件,则可以称为 奇偶树 : 二叉树根节点所在层下标为 0 ,根的子节点所在层下标为 1 ,根的孙节点所在层下标为 2 ,依此类推。偶数下标 层上的所有节点的值都…

关于怀旧

每次换季都正好对个人资产进行更新换代的时候,上一年收拾东西的心情还依然记得,只是今年更加伤感。 上一年的我,爱情甜蜜,生活丰富,实验室项目也开展得十分顺利,整理东西时每打开一个箱子,就惊叹…

数据的特征工程

数据的特征工程 1 什么是数据的特征工程 特征工程是将原始数据转换为更好地代表预测模型的潜在问题的特征的过程,从而提高了对未知数据的模型准确性。 特征工程的意义:将直接影响模型的预测结果。 2 数据的来源与类型 2.1 数据的来源 企业日益积累…

数据库实例:mysql与redis结合用户登录

加入redis后登录逻辑如下图,将图中nosql的位置换为redis即可 用户数据存的键为用户名,值为密码 将原来MySQL操作的代码封装到一个方法中,代码如下 defmysql_login(): #redis中没有则到mysql中查询 sqlselect upwd from py_users wher…

LeetCode 1610. 可见点的最大数目(atan2函数求夹角)

文章目录1. 题目2. 解题1. 题目 给你一个点数组 points 和一个表示角度的整数 angle ,你的位置是 location ,其中 location [posx, posy] 且 points[i] [xi, yi] 都表示 X-Y 平面上的整数坐标。 最开始,你面向东方进行观测。你 不能 进行…

[翻译]API Guides - Bound Services

官方文档原文地址:http://developer.android.com/guide/components/bound-services.html 一个Bound Service是一个客户端-服务器接口的服务。一个Bound Service允许组件(像activity)绑定一个service,发送请求,接受结果…